All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.api.policy.PolicyDirective Maven / Gradle / Ivy

There is a newer version: 4.3.15
Show newest version
// Copyright 2015-2021 Swim Inc.
//
// 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 swim.api.policy;

import swim.codec.Debug;
import swim.codec.Format;
import swim.codec.Output;
import swim.util.Murmur3;

public abstract class PolicyDirective implements Debug {

  PolicyDirective() {
    // sealed
  }

  public boolean isAllowed() {
    return false;
  }

  public boolean isDenied() {
    return false;
  }

  public boolean isForbidden() {
    return false;
  }

  public boolean isDefined() {
    return false;
  }

  public T get() {
    throw null;
  }

  public Policy policy() {
    return null;
  }

  public Object reason() {
    return null;
  }

  private static Allow allow;

  @SuppressWarnings("unchecked")
  public static  PolicyDirective allow() {
    if (PolicyDirective.allow == null) {
      PolicyDirective.allow = new Allow(null);
    }
    return (PolicyDirective) (PolicyDirective) PolicyDirective.allow;
  }

  @SuppressWarnings("unchecked")
  public static  PolicyDirective allow(T value) {
    if (value != null) {
      return new Allow(value);
    } else {
      return PolicyDirective.allow();
    }
  }

  public static  PolicyDirective deny(Policy policy, Object reason) {
    return new Deny(policy, reason);
  }

  public static  PolicyDirective deny(Policy policy) {
    return new Deny(policy, null);
  }

  public static  PolicyDirective deny(Object reason) {
    return new Deny(null, reason);
  }

  public static  PolicyDirective deny() {
    return new Deny(null, null);
  }

  public static  PolicyDirective forbid(Policy policy, Object reason) {
    return new Forbid(policy, reason);
  }

  public static  PolicyDirective forbid(Policy policy) {
    return new Forbid(policy, null);
  }

  public static  PolicyDirective forbid(Object reason) {
    return new Forbid(null, reason);
  }

  public static  PolicyDirective forbid() {
    return new Forbid(null, null);
  }

  static final class Allow extends PolicyDirective {

    final T value;

    Allow(T value) {
      this.value = value;
    }

    @Override
    public boolean isAllowed() {
      return true;
    }

    @Override
    public boolean isDefined() {
      return this.value != null;
    }

    @Override
    public T get() {
      return this.value;
    }

    @Override
    public boolean equals(Object other) {
      if (this == other) {
        return true;
      } else if (other instanceof Allow) {
        final Allow that = (Allow) other;
        return this.value == null ? that.value == null : this.value.equals(that.value);
      } else {
        return false;
      }
    }

    private static int hashSeed;

    @Override
    public int hashCode() {
      if (Allow.hashSeed == 0) {
        Allow.hashSeed = Murmur3.seed(Allow.class);
      }
      return Murmur3.mash(Murmur3.mix(Allow.hashSeed, Murmur3.hash(this.value)));
    }

    @Override
    public  Output debug(Output output) {
      output = output.write("PolicyDirective").write('.').write("allow").write('(');
      if (this.value != null) {
        output = output.debug(this.value);
      }
      output = output.write(')');
      return output;
    }

    @Override
    public String toString() {
      return Format.debug(this);
    }

  }

  static final class Deny extends PolicyDirective {

    final Policy policy;
    final Object reason;

    Deny(Policy policy, Object reason) {
      this.policy = policy;
      this.reason = reason;
    }

    @Override
    public boolean isDenied() {
      return true;
    }

    @Override
    public Policy policy() {
      return this.policy;
    }

    @Override
    public Object reason() {
      return this.reason;
    }

    @Override
    public boolean equals(Object other) {
      if (this == other) {
        return true;
      } else if (other instanceof Deny) {
        final Deny that = (Deny) other;
        return (this.policy == null ? that.policy == null : this.policy.equals(that.policy))
            && (this.reason == null ? that.reason == null : this.reason.equals(that.reason));
      } else {
        return false;
      }
    }

    private static int hashSeed;

    @Override
    public int hashCode() {
      if (Deny.hashSeed == 0) {
        Deny.hashSeed = Murmur3.seed(Deny.class);
      }
      return Murmur3.mash(Murmur3.mix(Murmur3.mix(Deny.hashSeed,
          Murmur3.hash(this.policy)), Murmur3.hash(this.reason)));
    }

    @Override
    public  Output debug(Output output) {
      output = output.write("PolicyDirective").write('.')
                     .write("deny").write('(').debug(this.policy);
      if (this.reason != null) {
        output = output.write(", ").debug(this.reason);
      }
      output = output.write(')');
      return output;
    }

    @Override
    public String toString() {
      return Format.debug(this);
    }

  }

  static final class Forbid extends PolicyDirective {

    final Policy policy;
    final Object reason;

    Forbid(Policy policy, Object reason) {
      this.policy = policy;
      this.reason = reason;
    }

    @Override
    public boolean isForbidden() {
      return true;
    }

    @Override
    public Policy policy() {
      return this.policy;
    }

    @Override
    public Object reason() {
      return this.reason;
    }

    @Override
    public boolean equals(Object other) {
      if (this == other) {
        return true;
      } else if (other instanceof Forbid) {
        final Forbid that = (Forbid) other;
        return (this.policy == null ? that.policy == null : this.policy.equals(that.policy))
            && (this.reason == null ? that.reason == null : this.reason.equals(that.reason));
      } else {
        return false;
      }
    }

    private static int hashSeed;

    @Override
    public int hashCode() {
      if (Forbid.hashSeed == 0) {
        Forbid.hashSeed = Murmur3.seed(Forbid.class);
      }
      return Murmur3.mash(Murmur3.mix(Murmur3.mix(Forbid.hashSeed,
          Murmur3.hash(this.policy)), Murmur3.hash(this.reason)));
    }

    @Override
    public  Output debug(Output output) {
      output = output.write("PolicyDirective").write('.')
                     .write("forbid").write('(').debug(this.policy);
      if (this.reason != null) {
        output = output.write(", ").debug(this.reason);
      }
      output = output.write(')');
      return output;
    }

    @Override
    public String toString() {
      return Format.debug(this);
    }

  }

}