Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DaoNotes |
|
| 1.7;1.7 | ||||
DaoNotes$NoteType |
|
| 1.7;1.7 |
1 | package org.jtheque.core.utils.db; | |
2 | ||
3 | import java.awt.Image; | |
4 | import java.util.Arrays; | |
5 | ||
6 | /* | |
7 | * This file is part of JTheque. | |
8 | * | |
9 | * JTheque is free software: you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation, either version 3 of the License. | |
12 | * | |
13 | * JTheque is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | /** | |
23 | * A data access object for the notes. | |
24 | * | |
25 | * @author Baptiste Wicht | |
26 | */ | |
27 | public final class DaoNotes { | |
28 | private Note[] notes; | |
29 | ||
30 | 2 | private static final DaoNotes INSTANCE = new DaoNotes(); |
31 | ||
32 | /** | |
33 | * Construct a new DaoNotes. This class isn't instanciable. | |
34 | */ | |
35 | private DaoNotes() { | |
36 | 2 | super(); |
37 | 2 | } |
38 | ||
39 | /** | |
40 | * Return the unique instance of the class. | |
41 | * | |
42 | * @return The singleton of DaoNotes. | |
43 | */ | |
44 | public static DaoNotes getInstance() { | |
45 | 16 | return INSTANCE; |
46 | } | |
47 | ||
48 | /** | |
49 | * Return all the notes of the dao. | |
50 | * | |
51 | * @return An array containing all the notes. | |
52 | */ | |
53 | public Note[] getNotes() { | |
54 | 2 | if (notes == null) { |
55 | 2 | loadNotes(); |
56 | } | |
57 | ||
58 | 2 | return Arrays.copyOf(notes, notes.length); |
59 | } | |
60 | ||
61 | /** | |
62 | * Return the note with the specific value. | |
63 | * | |
64 | * @param value The value of the note we want | |
65 | * @return The note with this value or <code>null</code> if we doesn't it. | |
66 | */ | |
67 | public Note getNote(NoteType value) { | |
68 | 14 | if (notes == null) { |
69 | 0 | loadNotes(); |
70 | } | |
71 | ||
72 | 14 | Note note = null; |
73 | ||
74 | 56 | for (Note n : notes) { |
75 | 56 | if (n.getValue() == value) { |
76 | 14 | note = n; |
77 | 14 | break; |
78 | } | |
79 | } | |
80 | ||
81 | 14 | return note; |
82 | } | |
83 | ||
84 | /** | |
85 | * Return the image for the specified note. The image is still buffered. | |
86 | * | |
87 | * @param note The note to get the image for. | |
88 | * @return The image for the note. | |
89 | */ | |
90 | public static Image getImage(Note note) { | |
91 | 0 | return NoteImageManager.getImage(note); |
92 | } | |
93 | ||
94 | /** | |
95 | * Load all the notes. | |
96 | */ | |
97 | private void loadNotes() { | |
98 | 2 | if (notes == null) { |
99 | 2 | notes = new NoteImpl[7]; |
100 | ||
101 | 2 | notes[0] = new NoteImpl(NoteType.NULL, "data.notes.null"); |
102 | 2 | notes[1] = new NoteImpl(NoteType.BAD, "data.notes.bad"); |
103 | 2 | notes[2] = new NoteImpl(NoteType.MIDDLE, "data.notes.middle"); |
104 | 2 | notes[3] = new NoteImpl(NoteType.GOOD, "data.notes.good"); |
105 | 2 | notes[4] = new NoteImpl(NoteType.VERYGOOD, "data.notes.verygood"); |
106 | 2 | notes[5] = new NoteImpl(NoteType.PERFECT, "data.notes.perfect"); |
107 | 2 | notes[6] = new NoteImpl(NoteType.UNDEFINED, "data.notes.undefined"); |
108 | } | |
109 | 2 | } |
110 | ||
111 | /** | |
112 | * Return the default note. | |
113 | * | |
114 | * @return The default note. | |
115 | */ | |
116 | public Note getDefaultNote() { | |
117 | 0 | return notes[6]; |
118 | } | |
119 | ||
120 | /** | |
121 | * A properties class for notes. | |
122 | * | |
123 | * @author Baptiste Wicht | |
124 | */ | |
125 | 18 | public enum NoteType { |
126 | 2 | ERROR(0), |
127 | 2 | NULL(1), |
128 | 2 | BAD(2), |
129 | 2 | MIDDLE(3), |
130 | 2 | GOOD(4), |
131 | 2 | VERYGOOD(5), |
132 | 2 | PERFECT(6), |
133 | 2 | UNDEFINED(7); |
134 | ||
135 | private final int note; | |
136 | ||
137 | /** | |
138 | * Construct a new NoteType. | |
139 | * | |
140 | * @param note The note value. | |
141 | */ | |
142 | 16 | NoteType(int note) { |
143 | 16 | this.note = note; |
144 | 16 | } |
145 | ||
146 | /** | |
147 | * Return the int value of the NoteType. | |
148 | * | |
149 | * @return The int value of the enum. | |
150 | */ | |
151 | public int intValue() { | |
152 | 56 | return note; |
153 | } | |
154 | ||
155 | /** | |
156 | * Return the enum with the enum int value. | |
157 | * | |
158 | * @param e The int value to search. | |
159 | * @return The NoteType corresponding to the int value to search. | |
160 | */ | |
161 | public static NoteType getEnum(int e) { | |
162 | 16 | NoteType note = MIDDLE; |
163 | ||
164 | 72 | for (NoteType n : values()) { |
165 | 72 | if (n.ordinal() == e) { |
166 | 16 | note = n; |
167 | 16 | break; |
168 | } | |
169 | } | |
170 | ||
171 | 16 | return note; |
172 | } | |
173 | } | |
174 | } |