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

io.vertx.ext.auth.authorization.impl.AuthorizationsImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0.CR5
Show newest version
/********************************************************************************
 * Copyright (c) 2019 Stephane Bastian
 *
 * This program and the accompanying materials are made available under the 2
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 3
 *
 * Contributors: 1
 *   Stephane Bastian - initial API and implementation
 ********************************************************************************/
package io.vertx.ext.auth.authorization.impl;

import io.vertx.ext.auth.authorization.Authorization;
import io.vertx.ext.auth.authorization.Authorizations;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class AuthorizationsImpl implements Authorizations {

  private Map> authorizations;

  @Override
  public synchronized Authorizations put(String providerId, Set _authorizations) {
    Objects.requireNonNull(providerId);
    Map> authorizations = this.authorizations;
    if (authorizations == null) {
      if (_authorizations != null) {
        authorizations = new HashMap<>();
      }
    } else {
      authorizations = new HashMap<>(authorizations);
      if (_authorizations == null) {
        authorizations.remove(providerId);
      }
    }
    if (_authorizations != null) {
      authorizations
        .put(providerId, Collections.unmodifiableSet(_authorizations));
    }

    // swap
    this.authorizations = authorizations;
    return this;
  }

  @Override
  public synchronized Authorizations putAll(Map> authorizations) {
    Objects.requireNonNull(authorizations);
    this.authorizations = authorizations;
    return this;
  }

  @Override
  public synchronized Authorizations clear() {
    authorizations = null;
    return this;
  }

  @Override
  public boolean contains(String providerId) {
    return authorizations != null && authorizations.containsKey(providerId);
  }

  @Override
  public boolean isEmpty() {
    final Map> authorizations = this.authorizations;
    return authorizations == null || authorizations.isEmpty();
  }

  @Override
  public boolean equals(Object obj) {
    final Map> authorizations = this.authorizations;

    if (this == obj)
      return true;
    if (!(obj instanceof AuthorizationsImpl))
      return false;
    AuthorizationsImpl other = (AuthorizationsImpl) obj;

    return Objects.equals(authorizations, other.authorizations);
  }

  @Override
  public int hashCode() {
    final Map> authorizations = this.authorizations;

    final int prime = 31;
    int result = 1;
    result = prime * result + Objects.hashCode(authorizations);
    return result;
  }

  @Override
  public boolean verify(Authorization resolvedAuthorization) {
    final Map> authorizations = this.authorizations;

    if (authorizations == null) {
      return false;
    }

    for (Map.Entry> kv : authorizations.entrySet()) {
      for (Authorization authorization : kv.getValue()) {
        if (authorization.verify(resolvedAuthorization)) {
          return true;
        }
      }
    }
    return false;
  }

  @Override
  public Authorizations forEach(BiConsumer consumer) {
    final Map> authorizations = this.authorizations;

    if (authorizations == null) {
      return this;
    }

    for (Map.Entry> kv : authorizations.entrySet()) {
      for (Authorization authorization : kv.getValue()) {
        consumer.accept(kv.getKey(), authorization);
      }
    }
    return this;
  }

  @Override
  public Authorizations forEach(String providerId, Consumer consumer) {
    final Map> authorizations = this.authorizations;

    if (authorizations == null) {
      return this;
    }

    authorizations
      .getOrDefault(providerId, Collections.emptySet())
      .forEach(consumer);
    return this;
  }

  @Override
  public String toString() {
    return authorizations == null ?
      "" :
      authorizations.toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy