001 package sexp; 002 003 /** 004 * SSymbol represents an expression consisting of a symbol, e.g. foobar. 005 */ 006 public class SSymbol implements SExp { 007 private final String value; 008 009 private void checkRep() { 010 assert value != null; 011 } 012 013 /** 014 * Make an SSymbol. 015 * @requires value != null 016 */ 017 public SSymbol(String value) { 018 if (value == null) throw new IllegalArgumentException("Argument cannot be null."); 019 this.value = value; 020 checkRep(); 021 } 022 023 /** 024 * @return symbol represented by this expression 025 */ 026 public String value() { return value; } 027 028 @Override 029 public String toString() { return value; } 030 031 public <T> T accept(SExpVisitor<T> visitor) { 032 return visitor.visit(this); 033 } 034 035 @Override 036 public int hashCode() { 037 final int prime = 31; 038 int result = 1; 039 result = prime * result + ((value == null) ? 0 : value.hashCode()); 040 return result; 041 } 042 043 @Override 044 public boolean equals(Object obj) { 045 if (this == obj) 046 return true; 047 if (obj == null) 048 return false; 049 if (getClass() != obj.getClass()) 050 return false; 051 final SSymbol other = (SSymbol) obj; 052 return value.equals(other.value); 053 } 054 }