io.neba.api.resourcemodels.Optional Maven / Gradle / Ivy
Show all versions of io.neba.neba-api Show documentation
/*
Copyright 2013 the original author or authors.
Licensed under the Apache License, Version 2.0 the "License";
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.neba.api.resourcemodels;
import java.util.NoSuchElementException;
/**
*
* This value-holder interface is used to declare lazy-loading 1:1 relationships in resource models. It is designed
* to be API-compatible to the JAVA 8 "Optional" interface
* to allow leveraging to the latter interface in the future.
*
*
* NEBA will automatically detect {@link Optional} members and provide a suitable lazy-loading implementation.
* Note that this interface is not required to lazy-load collections, as NEBA automatically
* provides collection-typed members, such as {@link io.neba.api.annotations.Reference}
* or {@link io.neba.api.annotations.Children} collections, as lazy-loading proxies.
*
*
* To declare a lazy reference from resource model "A" to resource model "B", write:
*
*
* @{@link io.neba.api.annotations.ResourceModel}(types = "...")
* public class A {
* @{@link io.neba.api.annotations.Reference}
* private Optional<B> b;
* }
*
*
* This interface may also be used to explicitly lazy-load collection-typed resource model relationships, such as
* {@link io.neba.api.annotations.Children} or {@link io.neba.api.annotations.Reference} collections:
*
*
* @{@link io.neba.api.annotations.ResourceModel}(types = "...")
* public class A {
* @{@link io.neba.api.annotations.Children}
* private Optional<List<B>> children;
* }
*
*
* However, collection-typed relationships are automatically provided as lazy-loading proxies, thus there usually is no
* reason to make them {@link io.neba.api.resourcemodels.Optional}.
*
*
* @deprecated Use {@link Lazy} instead.
* @param the type of the lazy-loaded object.
* @author Olaf Otto
*/
@Deprecated
public interface Optional {
/**
* @return the non-null
value, or throws a {@link java.util.NoSuchElementException} if no value exists.
* @throws NoSuchElementException if no value exists.
*/
T get() throws NoSuchElementException;
/**
* @param defaultValue can be null
.
* @return the value if non-null
, otherwise the default value, which can be null
.
*/
T orElse(T defaultValue);
/**
* @return true
if the value is non-null
. This method attempts to load the value. It
* is equivalent to invoking {@link #orElse(Object)} with a null
argument and checking
* whether the returned value is non-null
.
*/
boolean isPresent();
}