From 4e7f086f831d2be4422f21137b20b36340f91a5c Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sat, 16 Jul 2016 01:14:09 -0700 Subject: [PATCH] GUACAMOLE-5: Move common identifier equals() / hashCode() of abstract Identifiable implementations to common base class. --- .../net/auth/AbstractActiveConnection.java | 24 ++---- .../net/auth/AbstractAuthenticatedUser.java | 42 +---------- .../net/auth/AbstractConnection.java | 41 +--------- .../net/auth/AbstractConnectionGroup.java | 42 +---------- .../net/auth/AbstractIdentifiable.java | 75 +++++++++++++++++++ .../guacamole/net/auth/AbstractUser.java | 42 +---------- 6 files changed, 92 insertions(+), 174 deletions(-) create mode 100644 guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractIdentifiable.java diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractActiveConnection.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractActiveConnection.java index 4b3855f37..7b13bf4e1 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractActiveConnection.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractActiveConnection.java @@ -22,12 +22,14 @@ package org.apache.guacamole.net.auth; import java.util.Date; import org.apache.guacamole.net.GuacamoleTunnel; -public abstract class AbstractActiveConnection implements ActiveConnection { - - /** - * The identifier of this active connection. - */ - private String identifier; +/** + * Base implementation of an ActiveConnection, providing storage and simply + * getters/setters for its main properties. + * + * @author Michael Jumper + */ +public abstract class AbstractActiveConnection extends AbstractIdentifiable + implements ActiveConnection { /** * The identifier of the associated connection. @@ -54,16 +56,6 @@ public abstract class AbstractActiveConnection implements ActiveConnection { */ private GuacamoleTunnel tunnel; - @Override - public String getIdentifier() { - return identifier; - } - - @Override - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - @Override public String getConnectionIdentifier() { return connectionIdentifier; diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractAuthenticatedUser.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractAuthenticatedUser.java index b26fac549..3d6739dc6 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractAuthenticatedUser.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractAuthenticatedUser.java @@ -26,45 +26,9 @@ package org.apache.guacamole.net.auth; * * @author Michael Jumper */ -public abstract class AbstractAuthenticatedUser implements AuthenticatedUser { +public abstract class AbstractAuthenticatedUser extends AbstractIdentifiable + implements AuthenticatedUser { - /** - * The name of this user. - */ - private String username; - - @Override - public String getIdentifier() { - return username; - } - - @Override - public void setIdentifier(String username) { - this.username = username; - } - - @Override - public int hashCode() { - if (username == null) return 0; - return username.hashCode(); - } - - @Override - public boolean equals(Object obj) { - - // Not equal if null or not a User - if (obj == null) return false; - if (!(obj instanceof AbstractAuthenticatedUser)) return false; - - // Get username - String objUsername = ((AbstractAuthenticatedUser) obj).username; - - // If null, equal only if this username is null - if (objUsername == null) return username == null; - - // Otherwise, equal only if strings are identical - return objUsername.equals(username); - - } + // Prior functionality now resides within AbstractIdentifiable } diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnection.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnection.java index 93ad8f6be..e3c9af54e 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnection.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnection.java @@ -26,17 +26,14 @@ import org.apache.guacamole.protocol.GuacamoleConfiguration; * * @author Michael Jumper */ -public abstract class AbstractConnection implements Connection { +public abstract class AbstractConnection extends AbstractIdentifiable + implements Connection { /** * The name associated with this connection. */ private String name; - /** - * The unique identifier associated with this connection. - */ - private String identifier; /** * The unique identifier of the parent ConnectionGroup for @@ -59,16 +56,6 @@ public abstract class AbstractConnection implements Connection { this.name = name; } - @Override - public String getIdentifier() { - return identifier; - } - - @Override - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - @Override public String getParentIdentifier() { return parentIdentifier; @@ -89,28 +76,4 @@ public abstract class AbstractConnection implements Connection { this.configuration = configuration; } - @Override - public int hashCode() { - if (identifier == null) return 0; - return identifier.hashCode(); - } - - @Override - public boolean equals(Object obj) { - - // Not equal if null or not a Connection - if (obj == null) return false; - if (!(obj instanceof AbstractConnection)) return false; - - // Get identifier - String objIdentifier = ((AbstractConnection) obj).identifier; - - // If null, equal only if this identifier is null - if (objIdentifier == null) return identifier == null; - - // Otherwise, equal only if strings are identical - return objIdentifier.equals(identifier); - - } - } diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnectionGroup.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnectionGroup.java index 882ef1839..d5241fa7f 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnectionGroup.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractConnectionGroup.java @@ -24,18 +24,14 @@ package org.apache.guacamole.net.auth; * * @author James Muehlner */ -public abstract class AbstractConnectionGroup implements ConnectionGroup { +public abstract class AbstractConnectionGroup extends AbstractIdentifiable + implements ConnectionGroup { /** * The name associated with this connection group. */ private String name; - /** - * The unique identifier associated with this connection group. - */ - private String identifier; - /** * The unique identifier of the parent connection group for * this connection group. @@ -57,16 +53,6 @@ public abstract class AbstractConnectionGroup implements ConnectionGroup { this.name = name; } - @Override - public String getIdentifier() { - return identifier; - } - - @Override - public void setIdentifier(String identifier) { - this.identifier = identifier; - } - @Override public String getParentIdentifier() { return parentIdentifier; @@ -87,28 +73,4 @@ public abstract class AbstractConnectionGroup implements ConnectionGroup { this.type = type; } - @Override - public int hashCode() { - if (identifier == null) return 0; - return identifier.hashCode(); - } - - @Override - public boolean equals(Object obj) { - - // Not equal if null or not a ConnectionGroup - if (obj == null) return false; - if (!(obj instanceof AbstractConnectionGroup)) return false; - - // Get identifier - String objIdentifier = ((AbstractConnectionGroup) obj).identifier; - - // If null, equal only if this identifier is null - if (objIdentifier == null) return identifier == null; - - // Otherwise, equal only if strings are identical - return objIdentifier.equals(identifier); - - } - } diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractIdentifiable.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractIdentifiable.java new file mode 100644 index 000000000..60cd6050c --- /dev/null +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractIdentifiable.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.guacamole.net.auth; + + +/** + * Abstract implementation of Identifiable which provides equals() and + * hashCode() implementations which use the identifier to determine equality. + * The identifier comparison is case-sensitive. + * + * @author Michael Jumper + */ +public abstract class AbstractIdentifiable implements Identifiable { + + /** + * The unique string which identifies this object. + */ + private String identifier; + + @Override + public String getIdentifier() { + return identifier; + } + + @Override + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + @Override + public int hashCode() { + + if (identifier == null) + return 0; + + return identifier.hashCode(); + } + + @Override + public boolean equals(Object other) { + + // Not equal if null or not the same type of object + if (other == null || getClass() != other.getClass()) + return false; + + // Get identifier of other object + String otherIdentifier = ((AbstractIdentifiable) other).getIdentifier(); + + // If null, equal only if this identifier is null + if (otherIdentifier == null) + return identifier == null; + + // Otherwise, equal only if strings are identical + return otherIdentifier.equals(identifier); + + } + +} diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractUser.java b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractUser.java index 96defff98..6232bc673 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractUser.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/net/auth/AbstractUser.java @@ -26,12 +26,8 @@ package org.apache.guacamole.net.auth; * * @author Michael Jumper */ -public abstract class AbstractUser implements User { - - /** - * The name of this user. - */ - private String username; +public abstract class AbstractUser extends AbstractIdentifiable + implements User { /** * This user's password. Note that while this provides a means for the @@ -40,16 +36,6 @@ public abstract class AbstractUser implements User { */ private String password; - @Override - public String getIdentifier() { - return username; - } - - @Override - public void setIdentifier(String username) { - this.username = username; - } - @Override public String getPassword() { return password; @@ -60,28 +46,4 @@ public abstract class AbstractUser implements User { this.password = password; } - @Override - public int hashCode() { - if (username == null) return 0; - return username.hashCode(); - } - - @Override - public boolean equals(Object obj) { - - // Not equal if null or not a User - if (obj == null) return false; - if (!(obj instanceof AbstractUser)) return false; - - // Get username - String objUsername = ((AbstractUser) obj).username; - - // If null, equal only if this username is null - if (objUsername == null) return username == null; - - // Otherwise, equal only if strings are identical - return objUsername.equals(username); - - } - }