001/* 002 * BioJava development code 003 * 004 * This code may be freely distributed and modified under the 005 * terms of the GNU Lesser General Public Licence. This should 006 * be distributed with the code. If you do not have a copy, 007 * see: 008 * 009 * http://www.gnu.org/copyleft/lesser.html 010 * 011 * Copyright for this code is held jointly by the individual 012 * authors. These should be listed in @author doc comments. 013 * 014 * For more information on the BioJava project and its aims, 015 * or to join the biojava-l mailing list, visit the home page 016 * at: 017 * 018 * http://www.biojava.org/ 019 * 020 */ 021 022package org.biojavax; 023 024import org.biojava.utils.AbstractChangeable; 025import org.biojava.utils.ChangeEvent; 026import org.biojava.utils.ChangeSupport; 027import org.biojava.utils.ChangeVetoException; 028 029/** 030 * An implementaion of Comment. 031 * @author Richard Holland 032 * @author gwaldon 033 * @since 1.5 034 */ 035public class SimpleComment extends AbstractChangeable implements Comment { 036 037 private String comment; 038 private int rank; 039 040 /** 041 * Constructs a new, immutable comment, given some text and a rank. 042 * @param comment the text of the comment. Cannot be null. 043 * @param rank the rank of the comment. 044 */ 045 public SimpleComment(String comment, int rank) { 046 if (comment==null) throw new IllegalArgumentException("Comment cannot be null"); 047 this.comment = comment; 048 this.rank = rank; 049 } 050 051 // Hibernate requirement - not for public use. 052 protected SimpleComment() {} 053 054 // Hibernate requirement - not for public use. 055 protected void setComment(String comment) { this.comment = comment; } 056 057 /** 058 * {@inheritDoc} 059 */ 060 public String getComment() { return this.comment; } 061 062 /** 063 * {@inheritDoc} 064 */ 065 public void setRank(int rank) throws ChangeVetoException { 066 if(rank==this.rank) 067 return; 068 if(!this.hasListeners(Comment.RANK)) { 069 this.rank = rank; 070 } else { 071 ChangeEvent ce = new ChangeEvent( 072 this, 073 Comment.RANK, 074 new Integer(rank), 075 new Integer(this.rank) 076 ); 077 ChangeSupport cs = this.getChangeSupport(Comment.RANK); 078 synchronized(cs) { 079 cs.firePreChangeEvent(ce); 080 this.rank = rank; 081 cs.firePostChangeEvent(ce); 082 } 083 } 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 public int getRank() { return this.rank; } 090 091 /** 092 * {@inheritDoc} 093 * Two comments are defined as equal if their text values and 094 * rankings are identical. 095 */ 096 public boolean equals(Object obj) { 097 if (this == obj) return true; 098 if (obj==null || !(obj instanceof Comment)) return false; 099 // Hibernate comparison - we haven't been populated yet 100 if (this.comment==null) return false; 101 // Normal comparison 102 Comment them = (Comment)obj; 103 return (this.rank==them.getRank() && 104 this.comment.equals(them.getComment())); 105 } 106 107 /** 108 * {@inheritDoc} 109 * Comments are ordered first by their rank, then by a string 110 * comparison of their text values. 111 */ 112 public int compareTo(Object o) { 113 if (o==this) return 0; 114 // Hibernate comparison - we haven't been populated yet 115 if (this.comment==null) return -1; 116 // Normal comparison 117 Comment them = (Comment)o; 118 if (this.rank!=them.getRank()) return this.rank-them.getRank(); 119 return this.comment.compareTo(them.getComment()); 120 } 121 122 /** 123 * {@inheritDoc} 124 */ 125 public int hashCode() { 126 int code = 17; 127 // Hibernate comparison - we haven't been populated yet 128 if (this.comment==null) return code; 129 // Normal comparison 130 code = 37*code + this.comment.hashCode(); 131 code = 37*code + this.rank; 132 return code; 133 } 134 135 /** 136 * {@inheritDoc} 137 * Form: "(#rank) comment" 138 */ 139 public String toString() { 140 return "(#"+this.rank+") "+this.comment; 141 } 142 143 // Hibernate requirement - not for public use. 144 private Integer id; 145 146 /** 147 * Gets the Hibernate ID. Should be used with caution. 148 * @return the Hibernate ID, if using Hibernate. 149 */ 150 public Integer getId() { return this.id; } 151 152 /** 153 * Sets the Hibernate ID. Should be used with caution. 154 * @param id the Hibernate ID, if using Hibernate. 155 */ 156 public void setId(Integer id) { this.id = id;} 157}