/*
 * Copyright 2015 Higher Frequency Trading
 *
 *  http://www.higherfrequencytrading.com
 *
 *  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 net.openhft.chronicle.map;

import net.openhft.chronicle.hash.Value;
import net.openhft.chronicle.hash.replication.ReplicationContext;
import org.jetbrains.annotations.NotNull;

public class MaximizingValueMapEntryOperations<K, V extends Comparable<? super V>>
        implements MapEntryOperations<K, V> {

    @Override
    public boolean replaceValue(@NotNull MapEntry<K, V> entry, Value<V, ?> newValue) {
        if (entry.context() instanceof ReplicationContext) {
            if (newValue.get().compareTo(entry.value().get()) > 0) {
                // replace, if the new value is greater
                entry.doReplaceValue(newValue);
                return true;
            } else {
                // always fail, if the value came via replication is lesser than the current value.
                return false;
            }
        } else {
            return MapEntryOperations.super.replaceValue(entry, newValue);
        }
    }
}

@@ -729,16 +729,79 @@
 .Value;%0A
+import net.openhft.chronicle.hash.replication.ReplicatedEntry;%0A
 import n
@@ -858,16 +858,16 @@
 ontext;%0A
-
 import o
@@ -1144,27 +1144,71 @@
     
-if (entry.context()
+MapContext%3CK, V%3E context = entry.context();%0A        if (context
  ins
@@ -1250,19 +1250,35 @@
        i
-f (
+nt compareResult = 
 newValue
@@ -1318,12 +1318,216 @@
 t())
- %3E 0
+;%0A            if (compareResult %3E 0 %7C%7C (compareResult == 0 &&%0A                    ((ReplicationContext) context).remoteIdentifier() %3C=%0A                            ((ReplicatedEntry) entry).originIdentifier())
 ) %7B%0A

Make MaximizingValueMapEntryOperations eventually consistent