Class EntryIndexProxy<T>

java.lang.Object
com.exonum.binding.core.proxy.AbstractNativeProxy
com.exonum.binding.core.storage.indices.EntryIndexProxy<T>
Type Parameters:
T - the type of an element in this entry
All Implemented Interfaces:
StorageIndex

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 Details

    • 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.
    • get

      public T get()
      If value is present in the entry, returns it, otherwise, throws NoSuchElementException.
      Returns:
      a non-null value
      Throws:
      NoSuchElementException - if a value is not present in the Entry
      IllegalStateException - if the proxy is invalid
      IllegalArgumentException - if the supplied serializer cannot decode the value
    • remove

      public void remove()
      Removes a value from this entry.
      Throws:
      UnsupportedOperationException - if the entry is read-only.
      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()
    • getAddress

      public IndexAddress getAddress()
      Description copied from interface: StorageIndex
      Returns the index address: its unique identifier in the database. It consists of the name and, in case this index belongs to an index family, a family identifier.
      Specified by:
      getAddress in interface StorageIndex
    • toString

      public String toString()
      Overrides:
      toString in class Object