Class EntryIndexProxy<T>

  • Type Parameters:
    T - the type of an element in this entry

    public final class EntryIndexProxy<T>
    extends AbstractNativeProxy
    An Entry is a database index that can contain no or a single value.

    An Entry is analogous to Optional, but provides modifying ("destructive") operations when created with a Fork. Such methods are specified to throw UnsupportedOperationException if the entry is created with a Snapshot — a read-only database view.

    All method arguments are non-null by default.

    This class is not thread-safe and and its instances shall not be shared between threads.

    When the view goes out of scope, this entry is destroyed. Subsequent use of the closed entry is prohibited and will result in IllegalStateException.

    See Also:
    View
    • Method Detail

      • newInstance

        public static <E extends com.google.protobuf.MessageLite> EntryIndexProxy<E> newInstance​(String name,
                                                                                                 View view,
                                                                                                 Class<E> elementType)
        Creates a new Entry storing protobuf messages.
        Type Parameters:
        E - the type of entry; must be a protobuf message that has a static #parseFrom(byte[]) method
        Parameters:
        name - a unique alphanumeric non-empty identifier of the Entry in the underlying storage: [a-zA-Z0-9_]
        view - a database view. Must be valid. If a view is read-only, "destructive" operations are not permitted.
        elementType - the class of an element-protobuf message
        Throws:
        IllegalArgumentException - if the name is empty
        IllegalStateException - if the view proxy is invalid
      • newInstance

        public static <E> EntryIndexProxy<E> newInstance​(String name,
                                                         View view,
                                                         Serializer<E> serializer)
        Creates a new Entry.
        Parameters:
        name - a unique alphanumeric non-empty identifier of the Entry in the underlying storage: [a-zA-Z0-9_]
        view - a database view. Must be valid. If a view is read-only, "destructive" operations are not permitted.
        serializer - an entry serializer
        Throws:
        IllegalArgumentException - if the name is empty
        IllegalStateException - if the view proxy is invalid
        See Also:
        StandardSerializers
      • set

        public void set​(T value)
        Sets a new value of the entry, overwriting the previous value.
        Parameters:
        value - a value to set. Must not be null.
        Throws:
        UnsupportedOperationException - if the entry is read-only
        IllegalStateException - if the proxy is invalid
      • isPresent

        public boolean isPresent()
        Returns true if this entry exists in the database.
        Throws:
        IllegalStateException - if the proxy is invalid.
      • toOptional

        public Optional<T> toOptional()
        Converts the entry to Optional.

        Be aware that this method represents a state of the entry at the time of calling. And the returned value won't reflect the entry changes:

          
            entry.set("foo");
            Optional<String> optionalEntry = entry.toOptional();
            entry.remove();
            optionalEntry.get(); // -> returns "foo"
          
         
        Returns:
        Optional.of(value) if value is present in the entry, otherwise returns Optional.empty()
      • getName

        public final String getName()
        Returns the name of this index.