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(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(Object o) { 056 return str.equals(((StringIdentifier) o).toString()); 057 } 058 059 /** 060 * Returns a string representation of the object 061 * 062 * @return a string representation of the object 063 */ 064 public String toString() { 065 return str; 066 } 067 068 @Override 069 public int compareTo(Identifier o) { 070 if (o == null) { 071 if (str == null) 072 return 0; 073 return 1; 074 } else { 075 if (str == null) 076 return -1; 077 return str.compareTo(o.toString()); 078 } 079 } 080}