Source code: com/virtuosotechnologies/asaph/notationmanager/StandardInterval.java
1 /*
2 ================================================================================
3
4 FILE: StandardInterval.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 Standard implementation of Interval
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.Interval;
46 import com.virtuosotechnologies.asaph.model.notation.NotationFactory;
47
48
49 /**
50 * Standard implementation of Interval.
51 */
52 /*package*/ class StandardInterval
53 implements
54 Interval
55 {
56 private StandardNotationFactory factory_;
57 private int letterDiff_;
58 private int pitchDiff_;
59
60
61 /**
62 * Constructor
63 */
64 /*package*/ StandardInterval(
65 StandardNotationFactory factory,
66 int letterDiff,
67 int pitchDiff)
68 {
69 int jumps = letterDiff / 7;
70 letterDiff %= 7;
71 if (letterDiff < 0)
72 {
73 letterDiff += 7;
74 --jumps;
75 }
76 pitchDiff -= jumps*12;
77 letterDiff_ = letterDiff;
78 pitchDiff_ = pitchDiff;
79 factory_ = factory;
80 }
81
82
83 /**
84 * Get the NotationFactory that created this object
85 *
86 * @return the NotationFactory
87 */
88 public NotationFactory getNotationFactory()
89 {
90 return factory_;
91 }
92
93
94 /*package*/ int getLetterDiff()
95 {
96 return letterDiff_;
97 }
98
99
100 /*package*/ int getPitchDiff()
101 {
102 return pitchDiff_;
103 }
104
105
106 /**
107 * Get the negation of the interval
108 *
109 * @return negation
110 */
111 public Interval getNegated()
112 {
113 return new StandardInterval(factory_, -letterDiff_, -pitchDiff_);
114 }
115
116
117 /**
118 * Generate the long string representation
119 *
120 * @return String
121 */
122 public String generateLongString()
123 {
124 return factory_.getLongStringForInterval(this);
125 }
126
127
128 /**
129 * Generate the short string representation
130 *
131 * @return String
132 */
133 public String generateString()
134 {
135 return generateLongString();
136 }
137
138
139 /**
140 * equals
141 */
142 public boolean equals(
143 Object obj)
144 {
145 if (obj instanceof StandardInterval)
146 {
147 StandardInterval si = (StandardInterval)obj;
148 return factory_ == si.factory_ &&
149 letterDiff_ == si.letterDiff_ &&
150 pitchDiff_ == si.pitchDiff_;
151 }
152 return false;
153 }
154
155
156 /**
157 * hashCode
158 */
159 public int hashCode()
160 {
161 return factory_.hashCode()+letterDiff_+pitchDiff_*7;
162 }
163
164
165 /**
166 * toString
167 */
168 public String toString()
169 {
170 return generateLongString();
171 }
172 }