Source code: com/virtuosotechnologies/asaph/notationmanager/StandardNote.java
1 /*
2 ================================================================================
3
4 FILE: StandardNote.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Standard implementation of Note
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.notationmanager;
43
44
45 import com.virtuosotechnologies.asaph.model.notation.Note;
46 import com.virtuosotechnologies.asaph.model.notation.NoteModifier;
47 import com.virtuosotechnologies.asaph.model.notation.Interval;
48 import com.virtuosotechnologies.asaph.model.notation.NotationFactory;
49
50
51 /**
52 * Standard implementation of Note.
53 */
54 /*package*/ class StandardNote
55 implements
56 Note
57 {
58 private StandardNotationFactory factory_;
59 private int letterValue_;
60 private int pitchValue_;
61 private StandardNoteModifier modifier_;
62 private boolean isCaps_;
63
64
65 /*package*/ StandardNote(
66 StandardNotationFactory factory,
67 int letterValue,
68 int pitchValue,
69 StandardNoteModifier modifier,
70 boolean isCaps)
71 {
72 factory_ = factory;
73 letterValue_ = letterValue;
74 pitchValue_ = pitchValue;
75 modifier_ = modifier;
76 isCaps_ = isCaps;
77 }
78
79
80 /**
81 * Get the NotationFactory that created this object
82 *
83 * @return the NotationFactory
84 */
85 public NotationFactory getNotationFactory()
86 {
87 return factory_;
88 }
89
90
91 /*package*/ int getLetterValue()
92 {
93 return letterValue_;
94 }
95
96
97 /*package*/ int getPitchValue()
98 {
99 return pitchValue_;
100 }
101
102
103 /*package*/ boolean isCaps()
104 {
105 return isCaps_;
106 }
107
108
109 /**
110 * Generate the string representation
111 *
112 * @return String
113 */
114 public String generateString()
115 {
116 return factory_.getStringForNote(letterValue_, pitchValue_, modifier_, isCaps_);
117 }
118
119
120 /**
121 * Get the base of this note (that is, the note with the default modifier).
122 *
123 * @return the base Note
124 */
125 public Note getBaseNote()
126 {
127 return factory_.rawCreateNote(letterValue_, pitchValue_-modifier_.getValue(), 0, true);
128 }
129
130
131 /**
132 * Get the modifier of this note
133 *
134 * @return the NoteModifier
135 */
136 public NoteModifier getNoteModifier()
137 {
138 return modifier_;
139 }
140
141
142 /**
143 * Create a Note whose base is the same as this one, but with the given modifier.
144 *
145 * @param modifier new NoteModifier
146 * @return new Note
147 */
148 public Note getNoteWithModifier(
149 NoteModifier modifier)
150 {
151 return factory_.rawCreateNote(letterValue_, pitchValue_-modifier_.getValue(),
152 ((StandardNoteModifier)modifier).getValue(), isCaps_);
153 }
154
155
156 /**
157 * Create a note raised by an interval
158 *
159 * @param interval interval to raise by
160 * @return new Note
161 */
162 public Note getRaised(
163 Interval interval)
164 {
165 StandardInterval si = (StandardInterval)interval;
166 Note result = factory_.addIntervalToNote(letterValue_, pitchValue_, isCaps_,
167 si.getLetterDiff(), si.getPitchDiff());
168 return result;
169 }
170
171
172 /**
173 * Create a note lowered by an interval
174 *
175 * @param interval interval to lower by
176 * @return new Note
177 */
178 public Note getLowered(
179 Interval interval)
180 {
181 StandardInterval si = (StandardInterval)interval;
182 Note result = factory_.addIntervalToNote(letterValue_, pitchValue_, isCaps_,
183 -si.getLetterDiff(), -si.getPitchDiff());
184 return result;
185 }
186
187
188 /**
189 * Create an interval from this note to the given note
190 *
191 * @param note ending note
192 * @return new Interval
193 */
194 public Interval getIntervalTo(
195 Note note)
196 {
197 StandardNote sn = (StandardNote)note;
198 Interval result = new StandardInterval(factory_, sn.getLetterValue()-letterValue_,
199 sn.getPitchValue()-pitchValue_);
200 return result;
201 }
202
203
204 /**
205 * equals
206 */
207 public boolean equals(
208 Object obj)
209 {
210 if (obj instanceof StandardNote)
211 {
212 StandardNote sn = (StandardNote)obj;
213 return factory_ == sn.factory_ &&
214 letterValue_ == sn.letterValue_ &&
215 pitchValue_ == sn.pitchValue_;
216 }
217 return false;
218 }
219
220
221 /**
222 * hashCode
223 */
224 public int hashCode()
225 {
226 return factory_.hashCode() + letterValue_*16 + pitchValue_;
227 }
228
229
230 /**
231 * toString
232 */
233 public String toString()
234 {
235 return generateString();
236 }
237 }