@Override @SuppressWarnings("rawtypes") public final boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Map.Entry)) { return false; } Map.Entry otherEntry = (Map.Entry) other; return (ObjectUtils.nullSafeEquals(getKey(), otherEntry.getKey()) && ObjectUtils.nullSafeEquals(getValue(), otherEntry.getValue())); }
@Override @Nullable protected V execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries entries) { if (entry != null) { V oldValue = entry.getValue(); if (overwriteExisting) { entry.setValue(value); } return oldValue; } Assert.state(entries != null, "No entries segment"); entries.add(value); return null; } });
/** * A task that can be {@link Segment#doTask run} against a {@link Segment}. */ private abstract class Task<T> { private final EnumSet<TaskOption> options; public Task(TaskOption... options) { this.options = (options.length == 0 ? EnumSet.noneOf(TaskOption.class) : EnumSet.of(options[0], options)); } public boolean hasOption(TaskOption option) { return this.options.contains(option); } /** * Execute the task. * @param ref the found reference (or {@code null}) * @param entry the found entry (or {@code null}) * @param entries access to the underlying entries * @return the result of the task * @see #execute(Reference, Entry) */ @Nullable protected T execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries entries) { return execute(ref, entry); } /** * Convenience method that can be used for tasks that do not need access to {@link Entries}.
@Override @Nullable protected V execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries entries) { if (entry != null) { V oldValue = entry.getValue(); if (overwriteExisting) { entry.setValue(value); } return oldValue; } Assert.state(entries != null, "No entries segment"); entries.add(value); return null; } });
@Override @Nullable protected V execute(@Nullable Reference<K, V> reference, @Nullable Entry<K, V> entry, @Nullable Entries entries) { if (entry != null) { V oldValue = entry.getValue(); if (overwriteExisting) { entry.setValue(value); } return oldValue; } Assert.state(entries != null, "No entries segment"); entries.add(value); return null; } });
@Override @Nullable protected V execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry, @Nullable Entries entries) { if (entry != null) { V oldValue = entry.getValue(); if (overwriteExisting) { entry.setValue(value); } return oldValue; } Assert.state(entries != null, "No entries segment"); entries.add(value); return null; } });
@Override public boolean replace(K key, final V oldValue, final V newValue) { Boolean result = doTask(key, new Task<Boolean>(TaskOption.RESTRUCTURE_BEFORE, TaskOption.SKIP_IF_EMPTY) { @Override protected Boolean execute(@Nullable Reference<K, V> ref, @Nullable Entry<K, V> entry) { if (entry != null && ObjectUtils.nullSafeEquals(entry.getValue(), oldValue)) { entry.setValue(newValue); return true; } return false; } }); return (result == Boolean.TRUE); }
@Override @SuppressWarnings("rawtypes") public final boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Map.Entry)) { return false; } Map.Entry otherEntry = (Map.Entry) other; return (ObjectUtils.nullSafeEquals(getKey(), otherEntry.getKey()) && ObjectUtils.nullSafeEquals(getValue(), otherEntry.getValue())); }
@Override @SuppressWarnings("rawtypes") public final boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Map.Entry)) { return false; } Map.Entry otherEntry = (Map.Entry) other; return (ObjectUtils.nullSafeEquals(getKey(), otherEntry.getKey()) && ObjectUtils.nullSafeEquals(getValue(), otherEntry.getValue())); }
@Override @Nullable public V getOrDefault(@Nullable Object key, @Nullable V defaultValue) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null ? entry.getValue() : defaultValue); }
@Override @SuppressWarnings("rawtypes") public final boolean equals(Object other) { if (this == other) { return true; } if (!(other instanceof Map.Entry)) { return false; } Map.Entry otherEntry = (Map.Entry) other; return (ObjectUtils.nullSafeEquals(getKey(), otherEntry.getKey()) && ObjectUtils.nullSafeEquals(getValue(), otherEntry.getValue())); }
@Override public boolean containsKey(@Nullable Object key) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null && ObjectUtils.nullSafeEquals(entry.getKey(), key)); }
@Override @Nullable public V get(@Nullable Object key) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null ? entry.getValue() : null); }
@Override @Nullable public V get(@Nullable Object key) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null ? entry.getValue() : null); }
@Override @Nullable public V getOrDefault(@Nullable Object key, @Nullable V defaultValue) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null ? entry.getValue() : defaultValue); }
@Override protected Boolean execute(@Nullable Reference<K, V> reference, @Nullable Entry<K, V> entry) { if (entry != null && ObjectUtils.nullSafeEquals(entry.getValue(), value)) { if (reference != null) { reference.release(); } return true; } return false; } });
@Override @Nullable public V get(@Nullable Object key) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null ? entry.getValue() : null); }
@Override public boolean containsKey(@Nullable Object key) { Entry<K, V> entry = getEntryIfAvailable(key); return (entry != null && ObjectUtils.nullSafeEquals(entry.getKey(), key)); }