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

schemacrawler.crawl.MutablePrivilege Maven / Gradle / Ivy

There is a newer version: 16.24.2
Show newest version
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2020, Sualeh Fatehi .
All rights reserved.
------------------------------------------------------------------------

SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.

You may elect to redistribute this code under any of these licenses.

The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html

The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/

========================================================================
*/

package schemacrawler.crawl;


import static java.util.Comparator.naturalOrder;
import static us.fatehi.utility.Utility.isBlank;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import schemacrawler.schema.DatabaseObject;
import schemacrawler.schema.Grant;
import schemacrawler.schema.Privilege;

/**
 * Represents a privilege of a table or column.
 *
 * @author Sualeh Fatehi
 */
final class MutablePrivilege
  extends AbstractDependantObject
  implements Privilege
{

  private static final long serialVersionUID = -1117664231494271886L;


  private final class PrivilegeGrant
    implements Grant
  {

    private static final long serialVersionUID = 356151825191631484L;
    private final String grantee;
    private final String grantor;
    private final boolean isGrantable;

    PrivilegeGrant(final String grantor,
                   final String grantee,
                   final boolean isGrantable)
    {
      this.grantor = grantor;
      this.grantee = grantee;
      this.isGrantable = isGrantable;
    }

    @Override
    public int compareTo(final Grant otherGrant)
    {
      int compare = 0;
      if (compare == 0)
      {
        if (grantor == null)
        {
          return -1;
        }
        else
        {
          compare = grantor.compareTo(otherGrant.getGrantor());
        }
      }
      if (compare == 0)
      {
        if (grantee == null)
        {
          return -1;
        }
        else
        {
          compare = grantee.compareTo(otherGrant.getGrantee());
        }
      }
      return compare;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getGrantee()
    {
      return grantee;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getGrantor()
    {
      return grantor;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isGrantable()
    {
      return isGrantable;
    }

    @Override
    public MutablePrivilege getParent()
    {
      return MutablePrivilege.this;
    }

    @Override
    public int hashCode()
    {
      final int prime = 31;
      int result = 1;
      result = prime * result + getParent().hashCode();
      result = prime * result + (grantee == null? 0: grantee.hashCode());
      result = prime * result + (grantor == null? 0: grantor.hashCode());
      result = prime * result + (isGrantable? 1231: 1237);
      return result;
    }

    @Override
    public boolean equals(final Object obj)
    {
      if (this == obj)
      {
        return true;
      }
      if (obj == null)
      {
        return false;
      }
      if (getClass() != obj.getClass())
      {
        return false;
      }
      final PrivilegeGrant other = (PrivilegeGrant) obj;
      if (!getParent().equals(other.getParent()))
      {
        return false;
      }
      if (grantee == null)
      {
        if (other.grantee != null)
        {
          return false;
        }
      }
      else if (!grantee.equals(other.grantee))
      {
        return false;
      }
      if (grantor == null)
      {
        if (other.grantor != null)
        {
          return false;
        }
      }
      else if (!grantor.equals(other.grantor))
      {
        return false;
      }
      return isGrantable == other.isGrantable;
    }

    @Override
    public String toString()
    {
      return String.format("%s --> %s%s",
                           isBlank(grantor)? "": grantor,
                           isBlank(grantee)? "": grantee,
                           isGrantable? " (grantable)": "");
    }

  }


  private final Set> grants = new HashSet<>();

  MutablePrivilege(final DatabaseObjectReference parent, final String name)
  {
    super(parent, name);
  }

  @Override
  public Collection> getGrants()
  {
    final List> values = new ArrayList<>(grants);
    values.sort(naturalOrder());
    return values;
  }

  void addGrant(final String grantor,
                final String grantee,
                final boolean isGrantable)
  {
    if (!(isBlank(grantor) && isBlank(grantee)))
    {
      grants.add(new PrivilegeGrant(grantor, grantee, isGrantable));
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy