package org.vertexium.inmemory;

import org.vertexium.VertexiumException;
import org.vertexium.Visibility;
import org.vertexium.inmemory.security.Authorizations;
import org.vertexium.inmemory.security.ColumnVisibility;
import org.vertexium.inmemory.security.VisibilityEvaluator;
import org.vertexium.inmemory.security.VisibilityParseException;
import org.vertexium.util.ArrayUtils;
import org.vertexium.util.Preconditions;

import java.io.Serializable;
import java.util.Arrays;

import static org.vertexium.util.Preconditions.checkNotNull;

public class InMemoryAuthorizations implements org.vertexium.Authorizations, Serializable {
    private static final long serialVersionUID = 1L;
    private final String[] authorizations;

    public InMemoryAuthorizations(String... authorizations) {
        this.authorizations = authorizations;
    }

    @Override
    public String[] getAuthorizations() {
        return authorizations;
    }

    @Override
    public boolean equals(org.vertexium.Authorizations authorizations) {
        return ArrayUtils.intersectsAll(getAuthorizations(), authorizations.getAuthorizations());
    }

    @Override
    public String toString() {
        return Arrays.toString(authorizations);
    }

    @Override
    public boolean canRead(Visibility visibility) {
        Preconditions.checkNotNull(visibility, "visibility is required");

        // this is just a shortcut so that we don't need to construct evaluators and visibility objects to check for an empty string.
        if (visibility.getVisibilityString().length() == 0) {
            return true;
        }

        VisibilityEvaluator visibilityEvaluator = new VisibilityEvaluator(new Authorizations(this.getAuthorizations()));
        ColumnVisibility columnVisibility = new ColumnVisibility(visibility.getVisibilityString());
        try {
            return visibilityEvaluator.evaluate(columnVisibility);
        } catch (VisibilityParseException e) {
            throw new VertexiumException("could not evaluate visibility " + visibility.getVisibilityString(), e);
        }
    }
}

@@ -476,70 +476,8 @@
 s;%0A%0A
-import static org.vertexium.util.Preconditions.checkNotNull;%0A%0A
 publ
@@ -2013,11 +2013,474 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public boolean equals(Object o) %7B%0A        if (this == o) %7B%0A            return true;%0A        %7D%0A        if (o == null %7C%7C getClass() != o.getClass()) %7B%0A            return false;%0A        %7D%0A%0A        InMemoryAuthorizations that = (InMemoryAuthorizations) o;%0A%0A        return ArrayUtils.intersectsAll(getAuthorizations(), that.getAuthorizations());%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A        return Arrays.hashCode(authorizations);%0A    %7D
 %0A%7D%0A

add equals method to InMemoryAuthorizations