001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.io.network.util; 020 021import org.apache.reef.wake.ComparableIdentifier; 022import org.apache.reef.wake.Identifier; 023 024/** 025 * String identifier. 026 */ 027public class StringIdentifier implements ComparableIdentifier { 028 029 private final String str; 030 031 /** 032 * Constructs a string identifier. 033 * 034 * @param str a string 035 */ 036 StringIdentifier(final String str) { 037 this.str = str; 038 } 039 040 /** 041 * Returns a hash code for the object. 042 * 043 * @return a hash code value for this object 044 */ 045 public int hashCode() { 046 return str.hashCode(); 047 } 048 049 /** 050 * Checks that another object is equal to this object. 051 * 052 * @param o another object 053 * @return true if the object is the same as the object argument; false, otherwise 054 */ 055 public boolean equals(final Object o) { 056 if (this == o) { 057 return true; 058 } 059 if (o == null || getClass() != o.getClass()) { 060 return false; 061 } 062 return str.equals(((StringIdentifier) o).toString()); 063 } 064 065 /** 066 * Returns a string representation of the object. 067 * 068 * @return a string representation of the object 069 */ 070 public String toString() { 071 return str; 072 } 073 074 @Override 075 public int compareTo(final Identifier o) { 076 if (o == null) { 077 if (str == null) { 078 return 0; 079 } 080 return 1; 081 } else { 082 if (str == null) { 083 return -1; 084 } 085 return str.compareTo(o.toString()); 086 } 087 } 088}