package com.ilscipio.scipio.ce.util;

import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;

/**
 * Emulation of Java 8's {@link java.util.Optional}.
 * <p>
 * 2016: ABSOLUTELY REQUIRED in many cms data object classes to record lookups that returned
 * null values - to ensure preloadContent actually works properly and
 * don't get dangerous stray database lookups during render (which violate
 * the instance's caching and validity).
 */
@SuppressWarnings("serial")
public class SafeOptional<A> implements Serializable {
    private static final SafeOptional<?> EMPTY = new SafeOptional<>(null);

    private final A value;

    private SafeOptional(A value) {
        this.value = value;
    }

    public static <T> SafeOptional<T> of(T value) {
        return new SafeOptional<>(value);
    }

    public static <T> SafeOptional<T> ofNullable(T value) {
        return value == null ? empty() : of(value);
    }

    public static <T> SafeOptional<T> empty() {
        @SuppressWarnings("unchecked")
        SafeOptional<T> empty = (SafeOptional<T>) EMPTY;
        return empty;
    }

    public boolean isPresent() {
        return value != null;
    }

    public A get() {
        if (value == null) {
            throw new NoSuchElementException("No value present");
        }
        return value;
    }

    public A orElse(A other) {
        return value != null ? value : other;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof SafeOptional)) {
            return false;
        }

        SafeOptional<?> other = (SafeOptional<?>) obj;
        return Objects.equals(value, other.value);
    }
}
@@ -161,16 +161,24 @@
 %0A%0A/**%0A *
+ SCIPIO:
  Emulati
@@ -218,16 +218,45 @@
 ptional%7D
+ that implements Serializable
 .%0A * %3Cp%3E
@@ -690,20 +690,16 @@
 ional%3C%3E(
-null
 );%0A%0A    
@@ -720,16 +720,79 @@
 A value;
+%0A%0A    private SafeOptional() %7B%0A        this.value = null;%0A    %7D
 %0A%0A    pr
@@ -842,21 +842,45 @@
 value = 
+Objects.requireNonNull(
 value
+)
 ;%0A    %7D%0A
@@ -1690,33 +1690,32 @@
 true;%0A        %7D%0A
-%0A
         if (!(ob
@@ -1713,10 +1713,8 @@
  if 
-(!
 (obj
@@ -1742,48 +1742,170 @@
 nal)
-) %7B%0A            return false;%0A        %7D%0A
+ %7B%0A            SafeOptional%3C?%3E other = (SafeOptional%3C?%3E) obj;%0A            return Objects.equals(value, other.value);%0A        %7D else if (obj instanceof Optional) %7B
 %0A   
@@ -1901,36 +1901,36 @@
 onal) %7B%0A        
-Safe
+    
 Optional%3C?%3E othe
@@ -1926,36 +1926,32 @@
 nal%3C?%3E other = (
-Safe
 Optional%3C?%3E) obj
@@ -1944,32 +1944,36 @@
 tional%3C?%3E) obj;%0A
+    
         return O
@@ -1991,31 +1991,159 @@
 ls(value, other.
+orElse(null));%0A        %7D%0A        return false;%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return Objects.hashCode(
 value);%0A    %7D%0A%7D

use SafeOptional instead of Optional (serial fix)