001package jmri; 002 003import javax.annotation.CheckReturnValue; 004import javax.annotation.Nonnull; 005 006/** 007 * Utility class for managing access to a NamedBean. 008 * <p> 009 * This associates a particular name (either the user name or system name, 010 * typically) with a specific NamedBean. Later, when the user wants to do a 011 * rename operation, this is used to decide whether this particular reference 012 * should be renamed. Note, however, that these should only be created and 013 * access via the {@link NamedBeanHandleManager} instance. 014 * 015 * @param <T> the class of the NamedBean 016 * @see NamedBeanHandleManager 017 * @see NamedBean 018 * 019 * @author Bob Jacobsen Copyright 2009 020 */ 021public class NamedBeanHandle<T extends NamedBean> { 022 023 /** 024 * Create a handle to a particular bean accessed by a specific name. 025 * <p> 026 * Usually, defer to {@link NamedBeanHandleManager} to create these 027 * 028 * @param name the name for the handle 029 * @param bean the bean to handle 030 */ 031 public NamedBeanHandle(@Nonnull String name, @Nonnull T bean) { 032 this.name = name; 033 this.bean = bean; 034 } 035 036 @Nonnull 037 public String getName() { 038 return name; 039 } 040 041 @CheckReturnValue 042 public T getBean() { 043 return bean; 044 } 045 046 public void setBean(@Nonnull T bean) { 047 this.bean = bean; 048 } 049 050 public void setName(String name) { 051 this.name = name; 052 } 053 054 String name; 055 T bean; 056 057 @Override 058 @CheckReturnValue 059 public String toString() { 060 return "NamedBeanHandle named \"" + name + "\" for system name \"" + bean.getSystemName() + "\""; 061 } 062 063 @Override 064 @CheckReturnValue 065 public boolean equals(Object obj) { 066 if (obj == this) { 067 return true; 068 } 069 if (obj == null) { 070 return false; 071 } 072 073 if (!(getClass() == obj.getClass())) { 074 return false; 075 } else { 076 NamedBeanHandle<?> tmp = (NamedBeanHandle<?>) obj; 077 if (!tmp.getName().equals(this.getName())) { 078 return false; 079 } 080 if (tmp.getBean() != this.getBean()) { 081 return false; 082 } 083 } 084 return true; 085 } 086 087 @Override 088 @CheckReturnValue 089 public int hashCode() { 090 return 259 + getName().hashCode(); // 259 is arbitrary offset constant 091 } 092 093}