Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/notationmanager/StandardChord.java


1   /*
2   ================================================================================
3   
4     FILE:  StandardChord.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      Standard implementation of Chord
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.lib.util.ObjectUtils;
46  
47  import com.virtuosotechnologies.asaph.model.notation.Note;
48  import com.virtuosotechnologies.asaph.model.notation.Chord;
49  import com.virtuosotechnologies.asaph.model.notation.Interval;
50  import com.virtuosotechnologies.asaph.model.notation.NotationFactory;
51  
52  
53  /**
54   * Standard implementation of Chord
55   */
56  /*package*/ class StandardChord
57  implements
58    Chord
59  {
60    private StandardNotationFactory factory_;
61    private StandardNote mainNote_;
62    private String mainType_;
63    private StandardNote bassNote_;
64    private String bassType_;
65    
66    
67    /*package*/ StandardChord(
68      StandardNotationFactory factory,
69      StandardNote mainNote,
70      String mainType,
71      StandardNote bassNote,
72      String bassType)
73    {
74      factory_ = factory;
75      mainNote_ = mainNote;
76      mainType_ = mainType;
77      bassNote_ = bassNote;
78      bassType_ = bassType;
79    }
80    
81    
82    /**
83     * Get the NotationFactory that created this object
84     *
85     * @return the NotationFactory
86     */
87    public NotationFactory getNotationFactory()
88    {
89      return factory_;
90    }
91    
92    
93    /**
94     * Is the chord empty (i.e. does it generate the empty string?)
95     *
96     * @return true if the chord is empty, false if not
97     */
98    public boolean isEmpty()
99    {
100     return (mainNote_ == null && mainType_.length() == 0 && bassNote_ == null && bassType_.length() == 0);
101   }
102   
103   
104   /**
105    * Get the primary Note
106    *
107    * @return Note
108    */
109   public Note getNote()
110   {
111     return mainNote_;
112   }
113   
114   
115   /**
116    * Get the type string (e.g. "m" for minor, or "dim" for diminished)
117    *
118    * @return String
119    */
120   public String getType()
121   {
122     return mainType_;
123   }
124   
125   
126   /**
127    * Get the bass Note.
128    *
129    * @return Note
130    */
131   public Note getBass()
132   {
133     return bassNote_;
134   }
135   
136   
137   /**
138    * Get the bass type string. (Usually blank).
139    *
140    * @return String
141    */
142   public String getBassType()
143   {
144     return bassType_;
145   }
146   
147   
148   /**
149    * Generate the string
150    *
151    * @return String
152    */
153   public String generateString()
154   {
155     return factory_.getStringForChord(mainNote_, mainType_, bassNote_, bassType_);
156   }
157   
158   
159   /**
160    * Raise by an interval
161    *
162    * @param interval interval to raise by
163    * @return new Chord
164    */
165   public Chord getRaised(
166     Interval interval)
167   {
168     StandardNote mainNote = mainNote_;
169     StandardNote bassNote = bassNote_;
170     if (mainNote != null)
171     {
172       mainNote = (StandardNote)mainNote.getRaised(interval);
173     }
174     if (bassNote != null)
175     {
176       bassNote = (StandardNote)bassNote.getRaised(interval);
177     }
178     return new StandardChord(factory_, mainNote, mainType_, bassNote, bassType_);
179   }
180   
181   
182   /**
183    * Lower by an interval
184    *
185    * @param interval interval to lower by
186    * @return new Chord
187    */
188   public Chord getLowered(
189     Interval interval)
190   {
191     StandardNote mainNote = mainNote_;
192     StandardNote bassNote = bassNote_;
193     if (mainNote != null)
194     {
195       mainNote = (StandardNote)mainNote.getLowered(interval);
196     }
197     if (bassNote != null)
198     {
199       bassNote = (StandardNote)bassNote.getLowered(interval);
200     }
201     return new StandardChord(factory_, mainNote, mainType_, bassNote, bassType_);
202   }
203   
204   
205   /**
206    * equals
207    */
208   public boolean equals(
209     Object obj)
210   {
211     if (obj instanceof StandardChord)
212     {
213       StandardChord sc = (StandardChord)obj;
214       return factory_ == sc.factory_ &&
215         ObjectUtils.safeEquals(mainNote_, sc.mainNote_) &&
216         ObjectUtils.safeEquals(mainType_, sc.mainType_) &&
217         ObjectUtils.safeEquals(bassNote_, sc.bassNote_) &&
218         ObjectUtils.safeEquals(bassType_, sc.bassType_);
219     }
220     return false;
221   }
222   
223   
224   /**
225    * hashCode
226    */
227   public int hashCode()
228   {
229     int ret = factory_.hashCode();
230     if (mainNote_ != null)
231     {
232       ret += mainNote_.hashCode();
233     }
234     if (mainType_ != null)
235     {
236       ret += mainType_.hashCode();
237     }
238     if (bassNote_ != null)
239     {
240       ret += bassNote_.hashCode();
241     }
242     if (bassType_ != null)
243     {
244       ret += bassType_.hashCode();
245     }
246     return ret;
247   }
248   
249   
250   /**
251    * toString
252    */
253   public String toString()
254   {
255     return generateString();
256   }
257 }