package org.axonframework.eventsourcing.eventstore;

import org.axonframework.serialization.RevisionResolver;
import org.axonframework.serialization.SerializedType;

import static org.axonframework.common.ObjectUtils.getOrDefault;

/**
 * A simple {@link org.axonframework.serialization.RevisionResolver} based implementation of
 * SnapshotJury. Decides whether to use a snapshot based on the current aggregate revision and the current snapshot
 * revision. It simply does an 'equals' match and does not consider the order of revisions.
 *
 * @author Shyam Sankaran
 * @since 3.3
 */
public class RevisionBasedSnapshotJury implements SnapshotJury {

    private static final String NO_REVISION = "NO_REVISION";
    private final RevisionResolver resolver;

    /**
     * Initializes a RevisionBasedSnapshotJury with given {@code resolver}.
     *
     * @param resolver Resolver of type {@link org.axonframework.serialization.RevisionResolver } to resolve the
     *                 revision of aggregate corresponding to the snapshot
     */
    public RevisionBasedSnapshotJury(RevisionResolver resolver) {
        this.resolver = resolver;
    }

    @Override
    public boolean decide(DomainEventData<?> snapshot) {
        final SerializedType payloadType = snapshot.getPayload().getType();
        final String payloadRevision = getOrDefault(payloadType.getRevision(), NO_REVISION);
        final String aggregateRevision;
        try {
            aggregateRevision = getOrDefault(resolver.revisionOf(Class.forName(payloadType.getName())), NO_REVISION);
        } catch (ClassNotFoundException e) {
            // Payload type is not found, just ignore the snapshot.
            return false;
        }
        return payloadRevision.equals(aggregateRevision);
    }
}

@@ -155,24 +155,52 @@
 lizedType;%0A%0A
+import java.util.Optional;%0A%0A
 import stati
@@ -675,69 +675,8 @@
  %7B%0A%0A
-    private static final String NO_REVISION = %22NO_REVISION%22;%0A
     
@@ -1272,22 +1272,32 @@
   final 
+Optional%3C
 String
+%3E
  payload
@@ -1307,28 +1307,35 @@
 ision = 
-getOrDefault
+Optional.ofNullable
 (payload
@@ -1352,29 +1352,16 @@
 vision()
-, NO_REVISION
 );%0A     
@@ -1369,22 +1369,32 @@
   final 
+Optional%3C
 String
+%3E
  aggrega
@@ -1451,28 +1451,35 @@
 ision = 
-getOrDefault
+Optional.ofNullable
 (resolve
@@ -1532,21 +1532,8 @@
 ()))
-, NO_REVISION
 );%0A 

remove magic string by using an optional instead