Class EntryIndexProxy<T>
- java.lang.Object
-
- com.exonum.binding.proxy.AbstractNativeProxy
-
- com.exonum.binding.storage.indices.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 aFork
. Such methods are specified to throwUnsupportedOperationException
if the entry is created with aSnapshot
— 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
-
-
Field Summary
-
Fields inherited from class com.exonum.binding.proxy.AbstractNativeProxy
nativeHandle
-
-
Method Summary
Modifier and Type Method Description T
get()
If value is present in the entry, returns it, otherwise, throwsNoSuchElementException
.String
getName()
Returns the name of this index.boolean
isPresent()
Returns true if this entry exists in the database.static <E> EntryIndexProxy<E>
newInstance(String name, View view, Serializer<E> serializer)
Creates a new Entry.static <E extends com.google.protobuf.MessageLite>
EntryIndexProxy<E>newInstance(String name, View view, Class<E> elementType)
Creates a new Entry storing protobuf messages.void
remove()
Removes a value from this entry.void
set(T value)
Sets a new value of the entry, overwriting the previous value.Optional<T>
toOptional()
Converts the entry toOptional
.String
toString()
-
Methods inherited from class com.exonum.binding.proxy.AbstractNativeProxy
getNativeHandle, isValidHandle
-
-
-
-
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 emptyIllegalStateException
- 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 emptyIllegalStateException
- 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-onlyIllegalStateException
- 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, throwsNoSuchElementException
.- Returns:
- a non-null value
- Throws:
NoSuchElementException
- if a value is not present in the EntryIllegalStateException
- if the proxy is invalidIllegalArgumentException
- 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 toOptional
.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 returnsOptional.empty()
-
getName
public final String getName()
Returns the name of this index.
-
-