001 /**
002 * Copyright (c) 2010, SIB. All rights reserved.
003 *
004 * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005 * https://sourceforge.net/projects/javaprotlib/
006 *
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions are met:
009 * Redistributions of source code must retain the above copyright notice, this
010 * list of conditions and the following disclaimer. Redistributions in binary
011 * form must reproduce the above copyright notice, this list of conditions and
012 * the following disclaimer in the documentation and/or other materials provided
013 * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014 * its contributors may be used to endorse or promote products derived from this
015 * software without specific prior written permission.
016 *
017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020 * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 */
028 package org.expasy.jpl.commons.collection.symbol;
029
030
031 import java.io.Serializable;
032 import java.lang.reflect.Constructor;
033
034
035 /**
036 * The basic symbol class.
037 *
038 * @author nikitin
039 *
040 * @param <T> the symbol type.
041 *
042 * @version 1.0
043 *
044 */
045 public abstract class AbstractSymbol<T> implements Symbol<T>, Serializable {
046
047 private static final long serialVersionUID = -5042441402526578891L;
048
049 /** the symbol character */
050 protected char name;
051
052 /** the symbol type */
053 protected SymbolType<T> type;
054
055 /** for serialization only */
056 public AbstractSymbol() {}
057
058 protected AbstractSymbol(char name, SymbolType<T> type) {
059 this.name = name;
060 this.type = type;
061 }
062
063 @SuppressWarnings("unchecked")
064 public boolean equals(Object o) {
065
066 if (o instanceof AbstractSymbol) {
067 AbstractSymbol<T> sym = (AbstractSymbol) o;
068 if (sym.type.equals(type) && sym.name == name) {
069 return true;
070 }
071 }
072 return false;
073 }
074
075 public int hashCode() {
076 return name;
077 }
078
079 /** {@inheritDoc} */
080 public char getName() {
081 return name;
082 }
083
084 /** {@inheritDoc} */
085 public SymbolType<T> getType() {
086 return type;
087 }
088
089 @SuppressWarnings("unchecked")
090 public static <T> Symbol<T> newSymbolInstance(
091 Class<? extends Symbol<T>> cls, SymbolType<T> type, char c) {
092 try {
093 Constructor<? extends Symbol<T>> ct =
094 cls.getConstructor(char.class);
095 Object retobj = ct.newInstance(c);
096
097 ((AbstractSymbol<T>) retobj).type = type;
098
099 return (AbstractSymbol<T>) retobj;
100
101 } catch (Throwable e) {
102 System.err.println(e);
103 }
104 return null;
105 }
106
107 public String toString() {
108 StringBuilder sb = new StringBuilder();
109
110 sb.append(type.getName() + "." + name);
111
112 return sb.toString();
113 }
114
115 }