001: /*
002: * TableGrant.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import workbench.util.StringUtil;
015:
016: /**
017: * @author support@sql-workbench.net
018: */
019: public class TableGrant {
020: private TableIdentifier table;
021: private String grantee;
022: private String privilege;
023: private boolean grantable;
024: private int hashCode = 0;
025:
026: /**
027: * Create a new TableGrant.
028: * @param to which user received the grant. May not be null.
029: * @param what the privilege that was granted to the user <tt>to</tt>. May not be null.
030: * @param grantToOthers whether the user may grant the privilege to other users
031: */
032: public TableGrant(String to, String what, boolean grantToOthers) {
033: this .grantee = to;
034: this .privilege = what;
035: this .grantable = grantToOthers;
036:
037: StringBuilder b = new StringBuilder(30);
038: b.append(grantee);
039: b.append(privilege);
040: b.append(grantable);
041: hashCode = b.toString().hashCode();
042: }
043:
044: public int hashCode() {
045: return hashCode;
046: }
047:
048: public int compareTo(Object other) {
049: if (this .equals(other))
050: return 0;
051:
052: try {
053: TableGrant otherGrant = (TableGrant) other;
054: int c1 = grantee.compareToIgnoreCase(otherGrant.grantee);
055: int c2 = privilege
056: .compareToIgnoreCase(otherGrant.privilege);
057: if (c1 == 0) {
058: if (c2 == 0) {
059: if (grantable && !otherGrant.grantable)
060: return 1;
061: return -1;
062: } else {
063: return c2;
064: }
065: } else {
066: return c1;
067: }
068: } catch (ClassCastException e) {
069: return -1;
070: }
071: }
072:
073: public boolean equals(Object other) {
074: try {
075: TableGrant otherGrant = (TableGrant) other;
076: return StringUtil.equalStringIgnoreCase(grantee,
077: otherGrant.grantee)
078: && StringUtil.equalStringIgnoreCase(privilege,
079: otherGrant.privilege)
080: && grantable == otherGrant.grantable;
081: } catch (ClassCastException e) {
082: return false;
083: }
084: }
085:
086: public String toString() {
087: return "GRANT " + privilege + " TO " + grantee;
088: }
089:
090: public String getGrantee() {
091: return grantee;
092: }
093:
094: public String getPrivilege() {
095: return privilege;
096: }
097:
098: public boolean isGrantable() {
099: return grantable;
100: }
101: }
|