79 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.toy.anagrams/classes/com/toy/anagrams/ui/About.java:
--------------------------------------------------------------------------------
1 | package com.toy.anagrams.ui;
2 |
3 | import java.awt.Dimension;
4 | import java.awt.Point;
5 | import java.awt.Rectangle;
6 | import java.awt.event.ActionListener;
7 | import javax.swing.JDialog;
8 | import javax.swing.JFrame;
9 |
10 | /**
11 | * About dialog of the Anagram Game application.
12 | */
13 | public class About extends JDialog {
14 |
15 | /** Creates new form About */
16 | public About(JFrame parent) {
17 | super(parent,true);
18 | initComponents();
19 | pack();
20 | Rectangle parentBounds = parent.getBounds();
21 | Dimension size = getSize();
22 | // Center in the parent
23 | int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
24 | int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
25 | setLocation(new Point(x, y));
26 | }
27 |
28 | /** This method is called from within the constructor to
29 | * initialize the form.
30 | * WARNING: Do NOT modify this code. The content of this method is
31 | * always regenerated by the Form Editor.
32 | */
33 | private void initComponents() {//GEN-BEGIN:initComponents
34 | java.awt.GridBagConstraints gridBagConstraints;
35 |
36 | mainPanel = new javax.swing.JPanel();
37 | copyrightTextArea = new javax.swing.JTextArea();
38 | closeButton = new javax.swing.JButton();
39 |
40 | getContentPane().setLayout(new java.awt.GridBagLayout());
41 |
42 | setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
43 | setTitle("About Anagrams");
44 | mainPanel.setLayout(new java.awt.GridBagLayout());
45 |
46 | mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(11, 11, 12, 12)));
47 | copyrightTextArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
48 | copyrightTextArea.setColumns(25);
49 | copyrightTextArea.setEditable(false);
50 | copyrightTextArea.setLineWrap(true);
51 | copyrightTextArea.setRows(8);
52 | copyrightTextArea.setText("Anagrams\n\nCopyright (c) 2003 Irritable Enterprises, Inc.");
53 | copyrightTextArea.setWrapStyleWord(true);
54 | copyrightTextArea.setBorder(null);
55 | copyrightTextArea.setFocusable(false);
56 | gridBagConstraints = new java.awt.GridBagConstraints();
57 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
58 | gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
59 | gridBagConstraints.weightx = 1.0;
60 | gridBagConstraints.weighty = 1.0;
61 | gridBagConstraints.insets = new java.awt.Insets(24, 0, 24, 0);
62 | mainPanel.add(copyrightTextArea, gridBagConstraints);
63 |
64 | closeButton.setMnemonic('C');
65 | closeButton.setText("Close");
66 | closeButton.addActionListener(new java.awt.event.ActionListener() {
67 | public void actionPerformed(java.awt.event.ActionEvent evt) {
68 | closeButtonActionPerformed(evt);
69 | }
70 | });
71 |
72 | gridBagConstraints = new java.awt.GridBagConstraints();
73 | gridBagConstraints.gridx = 0;
74 | gridBagConstraints.gridy = 1;
75 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
76 | mainPanel.add(closeButton, gridBagConstraints);
77 |
78 | gridBagConstraints = new java.awt.GridBagConstraints();
79 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
80 | gridBagConstraints.weightx = 1.0;
81 | gridBagConstraints.weighty = 1.0;
82 | getContentPane().add(mainPanel, gridBagConstraints);
83 |
84 | }//GEN-END:initComponents
85 |
86 | private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
87 | setVisible(false);
88 | dispose();
89 | }//GEN-LAST:event_closeButtonActionPerformed
90 |
91 |
92 | // Variables declaration - do not modify//GEN-BEGIN:variables
93 | private javax.swing.JButton closeButton;
94 | private javax.swing.JTextArea copyrightTextArea;
95 | private javax.swing.JPanel mainPanel;
96 | // End of variables declaration//GEN-END:variables
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.toy.anagrams/classes/com/toy/anagrams/ui/Anagrams.form:
--------------------------------------------------------------------------------
1 |
2 |
3 |
214 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.toy.anagrams/classes/com/toy/anagrams/ui/Anagrams.java:
--------------------------------------------------------------------------------
1 |
2 | /* Anagram Game Application */
3 |
4 | package com.toy.anagrams.ui;
5 |
6 | import com.wordlibrary.WordLibrary;
7 | import java.awt.Dimension;
8 | import java.awt.Point;
9 | import java.awt.Toolkit;
10 | import java.util.List;
11 | import javax.swing.DefaultComboBoxModel;
12 | import javax.swing.JFrame;
13 | import javax.swing.SwingUtilities;
14 |
15 | /**
16 | * Main window of the Anagram Game application.
17 | */
18 | public class Anagrams extends JFrame {
19 |
20 | public static void main(String[] args) {
21 | /* Set the Nimbus look and feel */
22 | //
23 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
24 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
25 | */
26 | try {
27 | javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels();
28 | for (int idx=0; idx
43 | //
44 |
45 | /* Create and display the form */
46 | SwingUtilities.invokeLater(new Runnable() {
47 | public void run() {
48 | new Anagrams().setVisible(true);
49 | }
50 | });
51 | }
52 |
53 | private int wordIdx = 0;
54 |
55 | private DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
56 |
57 |
58 | // private WordLibrary wordLibrary;
59 |
60 | /** Creates new form Anagrams */
61 | public Anagrams() {
62 |
63 | List libraries = WordLibrary.openWordLibraries();
64 |
65 | initComponents();
66 |
67 | for (WordLibrary library : libraries) {
68 | dcbm.addElement(library);
69 | }
70 |
71 | category.setModel(dcbm);
72 |
73 | getRootPane().setDefaultButton(guessButton);
74 | pack();
75 | guessedWord.requestFocusInWindow();
76 | // Center in the screen
77 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
78 | Dimension frameSize = getSize();
79 | setLocation(new Point((screenSize.width - frameSize.width) / 2,
80 | (screenSize.height - frameSize.width) / 2));
81 |
82 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
83 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
84 | }
85 |
86 | /** This method is called from within the constructor to
87 | * initialize the form.
88 | * WARNING: Do NOT modify this code. The content of this method is
89 | * always regenerated by the Form Editor.
90 | */
91 | // //GEN-BEGIN:initComponents
92 | private void initComponents() {
93 | java.awt.GridBagConstraints gridBagConstraints;
94 |
95 | mainPanel = new javax.swing.JPanel();
96 | scrambledLabel = new javax.swing.JLabel();
97 | scrambledWord = new javax.swing.JTextField();
98 | guessLabel = new javax.swing.JLabel();
99 | guessedWord = new javax.swing.JTextField();
100 | feedbackLabel = new javax.swing.JLabel();
101 | buttonsPanel = new javax.swing.JPanel();
102 | guessButton = new javax.swing.JButton();
103 | nextTrial = new javax.swing.JButton();
104 | category = new javax.swing.JComboBox<>();
105 | jLabel1 = new javax.swing.JLabel();
106 | mainMenu = new javax.swing.JMenuBar();
107 | fileMenu = new javax.swing.JMenu();
108 | aboutMenuItem = new javax.swing.JMenuItem();
109 | exitMenuItem = new javax.swing.JMenuItem();
110 |
111 | setTitle("Anagrams");
112 | addWindowListener(new java.awt.event.WindowAdapter() {
113 | public void windowClosing(java.awt.event.WindowEvent evt) {
114 | exitForm(evt);
115 | }
116 | });
117 |
118 | mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
119 | mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
120 | mainPanel.setLayout(new java.awt.GridBagLayout());
121 |
122 | scrambledLabel.setText("Scrambled Word:");
123 | gridBagConstraints = new java.awt.GridBagConstraints();
124 | gridBagConstraints.gridx = 0;
125 | gridBagConstraints.gridy = 1;
126 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
127 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
128 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
129 | mainPanel.add(scrambledLabel, gridBagConstraints);
130 |
131 | scrambledWord.setEditable(false);
132 | scrambledWord.setColumns(20);
133 | gridBagConstraints = new java.awt.GridBagConstraints();
134 | gridBagConstraints.gridx = 1;
135 | gridBagConstraints.gridy = 1;
136 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
137 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
138 | gridBagConstraints.weightx = 1.0;
139 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
140 | mainPanel.add(scrambledWord, gridBagConstraints);
141 |
142 | guessLabel.setDisplayedMnemonic('Y');
143 | guessLabel.setLabelFor(guessedWord);
144 | guessLabel.setText("Your Guess:");
145 | gridBagConstraints = new java.awt.GridBagConstraints();
146 | gridBagConstraints.gridx = 0;
147 | gridBagConstraints.gridy = 2;
148 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
149 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
150 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
151 | mainPanel.add(guessLabel, gridBagConstraints);
152 |
153 | guessedWord.setColumns(20);
154 | gridBagConstraints = new java.awt.GridBagConstraints();
155 | gridBagConstraints.gridx = 1;
156 | gridBagConstraints.gridy = 2;
157 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
158 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
159 | gridBagConstraints.weightx = 1.0;
160 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
161 | mainPanel.add(guessedWord, gridBagConstraints);
162 |
163 | feedbackLabel.setText(" ");
164 | gridBagConstraints = new java.awt.GridBagConstraints();
165 | gridBagConstraints.gridx = 1;
166 | gridBagConstraints.gridy = 3;
167 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
168 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
169 | gridBagConstraints.weightx = 1.0;
170 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
171 | mainPanel.add(feedbackLabel, gridBagConstraints);
172 |
173 | buttonsPanel.setLayout(new java.awt.GridBagLayout());
174 |
175 | guessButton.setMnemonic('G');
176 | guessButton.setText("Guess");
177 | guessButton.setToolTipText("Guess the scrambled word.");
178 | guessButton.addActionListener(new java.awt.event.ActionListener() {
179 | public void actionPerformed(java.awt.event.ActionEvent evt) {
180 | guessedWordActionPerformed(evt);
181 | }
182 | });
183 | gridBagConstraints = new java.awt.GridBagConstraints();
184 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
185 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
186 | gridBagConstraints.weightx = 1.0;
187 | gridBagConstraints.weighty = 1.0;
188 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
189 | buttonsPanel.add(guessButton, gridBagConstraints);
190 |
191 | nextTrial.setMnemonic('N');
192 | nextTrial.setText("New Word");
193 | nextTrial.setToolTipText("Fetch a new word.");
194 | nextTrial.addActionListener(new java.awt.event.ActionListener() {
195 | public void actionPerformed(java.awt.event.ActionEvent evt) {
196 | nextTrialActionPerformed(evt);
197 | }
198 | });
199 | gridBagConstraints = new java.awt.GridBagConstraints();
200 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
201 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
202 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
203 | gridBagConstraints.weighty = 1.0;
204 | buttonsPanel.add(nextTrial, gridBagConstraints);
205 |
206 | gridBagConstraints = new java.awt.GridBagConstraints();
207 | gridBagConstraints.gridx = 0;
208 | gridBagConstraints.gridy = 4;
209 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
210 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
211 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
212 | gridBagConstraints.weighty = 1.0;
213 | mainPanel.add(buttonsPanel, gridBagConstraints);
214 |
215 | category.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
216 | category.addActionListener(new java.awt.event.ActionListener() {
217 | public void actionPerformed(java.awt.event.ActionEvent evt) {
218 | categoryActionPerformed(evt);
219 | }
220 | });
221 | gridBagConstraints = new java.awt.GridBagConstraints();
222 | gridBagConstraints.gridx = 1;
223 | gridBagConstraints.gridy = 0;
224 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
225 | gridBagConstraints.ipady = 6;
226 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
227 | mainPanel.add(category, gridBagConstraints);
228 |
229 | jLabel1.setText("Category:");
230 | gridBagConstraints = new java.awt.GridBagConstraints();
231 | gridBagConstraints.gridx = 0;
232 | gridBagConstraints.gridy = 0;
233 | gridBagConstraints.ipady = 6;
234 | gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
235 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
236 | mainPanel.add(jLabel1, gridBagConstraints);
237 |
238 | getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
239 |
240 | fileMenu.setMnemonic('F');
241 | fileMenu.setText("File");
242 |
243 | aboutMenuItem.setMnemonic('A');
244 | aboutMenuItem.setText("About");
245 | aboutMenuItem.setToolTipText("About");
246 | aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
247 | public void actionPerformed(java.awt.event.ActionEvent evt) {
248 | aboutMenuItemActionPerformed(evt);
249 | }
250 | });
251 | fileMenu.add(aboutMenuItem);
252 |
253 | exitMenuItem.setMnemonic('E');
254 | exitMenuItem.setText("Exit");
255 | exitMenuItem.setToolTipText("Quit Team, Quit!");
256 | exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
257 | public void actionPerformed(java.awt.event.ActionEvent evt) {
258 | exitMenuItemActionPerformed(evt);
259 | }
260 | });
261 | fileMenu.add(exitMenuItem);
262 |
263 | mainMenu.add(fileMenu);
264 |
265 | setJMenuBar(mainMenu);
266 | }// //GEN-END:initComponents
267 |
268 | private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
269 | new About(this).setVisible(true);
270 | }//GEN-LAST:event_aboutMenuItemActionPerformed
271 |
272 | private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed
273 |
274 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
275 |
276 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
277 |
278 | wordIdx = (wordIdx + 1) % wordLibrary.getSize();
279 |
280 | feedbackLabel.setText(" ");
281 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
282 | guessedWord.setText("");
283 | getRootPane().setDefaultButton(guessButton);
284 |
285 | guessedWord.requestFocusInWindow();
286 | }//GEN-LAST:event_nextTrialActionPerformed
287 |
288 | private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
289 | System.exit(0);
290 | }//GEN-LAST:event_exitMenuItemActionPerformed
291 |
292 | private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed
293 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
294 |
295 | if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
296 | feedbackLabel.setText("Correct! Try a new word!");
297 | getRootPane().setDefaultButton(nextTrial);
298 | } else {
299 | feedbackLabel.setText("Incorrect! Try again!");
300 | guessedWord.setText("");
301 | }
302 |
303 | guessedWord.requestFocusInWindow();
304 | }//GEN-LAST:event_guessedWordActionPerformed
305 |
306 | private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
307 | System.exit(0);
308 | }//GEN-LAST:event_exitForm
309 |
310 | private void categoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_categoryActionPerformed
311 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
312 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
313 | }//GEN-LAST:event_categoryActionPerformed
314 |
315 | // Variables declaration - do not modify//GEN-BEGIN:variables
316 | private javax.swing.JMenuItem aboutMenuItem;
317 | private javax.swing.JPanel buttonsPanel;
318 | private javax.swing.JComboBox category;
319 | private javax.swing.JMenuItem exitMenuItem;
320 | private javax.swing.JLabel feedbackLabel;
321 | private javax.swing.JMenu fileMenu;
322 | private javax.swing.JButton guessButton;
323 | private javax.swing.JLabel guessLabel;
324 | private javax.swing.JTextField guessedWord;
325 | private javax.swing.JLabel jLabel1;
326 | private javax.swing.JMenuBar mainMenu;
327 | private javax.swing.JPanel mainPanel;
328 | private javax.swing.JButton nextTrial;
329 | private javax.swing.JLabel scrambledLabel;
330 | private javax.swing.JTextField scrambledWord;
331 | // End of variables declaration//GEN-END:variables
332 |
333 | }
334 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.toy.anagrams/classes/module-info.java:
--------------------------------------------------------------------------------
1 | module com.toy.anagrams {
2 | requires java.base;
3 | requires java.desktop;
4 | requires java.logging;
5 | requires com.wordlibrary;
6 | }
7 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/com/wordlibrary/WordLibrary.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary;
2 |
3 | import com.wordlibrary.spi.WordLibraryProvider;
4 | import java.util.ArrayList;
5 | import java.util.Iterator;
6 | import java.util.List;
7 | import java.util.ServiceLoader;
8 |
9 | public abstract class WordLibrary {
10 |
11 | /**
12 | * Constructor for subclasses.
13 | */
14 | protected WordLibrary() {
15 | }
16 |
17 | static List wordLibraryList = new ArrayList();
18 |
19 | public static List openWordLibraries() {
20 | ServiceLoader sl
21 | = ServiceLoader.load(WordLibraryProvider.class);
22 | Iterator iter = sl.iterator();
23 | if (!iter.hasNext()) {
24 | throw new RuntimeException("No service providers found!");
25 | }
26 | while (iter.hasNext()) {
27 | WordLibraryProvider provider = iter.next();
28 | wordLibraryList.add(provider.openWordLibrary());
29 | }
30 | return wordLibraryList;
31 | }
32 |
33 | /**
34 | * Gets the word at a given index.
35 | *
36 | * @param idx index of required word
37 | * @return word at that index in its natural form
38 | */
39 | public abstract String getWord(int idx);
40 |
41 | /**
42 | * Gets the word at a given index in its scrambled form.
43 | *
44 | * @param idx index of required word
45 | * @return word at that index in its scrambled form
46 | */
47 | public abstract String getScrambledWord(int idx);
48 |
49 | /**
50 | * Gets the number of words in the library.
51 | *
52 | * @return the total number of plain/scrambled word pairs in the library
53 | */
54 | public abstract int getSize();
55 |
56 | /**
57 | * Checks whether a user's guess for a word at the given index is correct.
58 | *
59 | * @param idx index of the word guessed
60 | * @param userGuess the user's guess for the actual word
61 | * @return true if the guess was correct; false otherwise
62 | */
63 | public abstract boolean isCorrect(int idx, String userGuess);
64 | }
65 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/com/wordlibrary/spi/WordLibraryProvider.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.spi;
2 |
3 | import com.wordlibrary.WordLibrary;
4 |
5 | public abstract class WordLibraryProvider {
6 |
7 | protected WordLibraryProvider() {
8 | }
9 |
10 | public abstract WordLibrary openWordLibrary();
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/com/wordlibrary/utils/ArrayUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.lang.reflect.Array;
4 | import java.util.Arrays;
5 |
6 | /**
7 | * Handy utilities for working with Java arrays.
8 | *
9 | * @author Ken Partlow
10 | * @author John DeRegnaucourt (john@cedarsoftware.com)
11 | *
12 | * Copyright (c) Cedar Software LLC
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
15 | * use this file except in compliance with the License. You may obtain a copy of
16 | * the License at
17 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
23 | * License for the specific language governing permissions and limitations under
24 | * the License.
25 | */
26 | public final class ArrayUtilities {
27 |
28 | /**
29 | * Immutable common arrays.
30 | */
31 | public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
32 | public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
33 |
34 | /**
35 | * Private constructor to promote using as static class.
36 | */
37 | private ArrayUtilities() {
38 | super();
39 | }
40 |
41 | /**
42 | * This is a null-safe isEmpty check. It uses the Array static class for
43 | * doing a length check. This check is actually .0001 ms slower than the
44 | * following typed check:
45 | *
48 | * but gives you more flexibility, since it checks for all array types.
49 | *
50 | * @param array array to check
51 | * @return true if empty or null
52 | */
53 | public static boolean isEmpty(final Object array) {
54 | return array == null || Array.getLength(array) == 0;
55 | }
56 |
57 | /**
58 | * This is a null-safe size check. It uses the Array static class for doing
59 | * a length check. This check is actually .0001 ms slower than the following
60 | * typed check:
61 | *
77 | * The objects in the array are not cloned, thus there is no special
78 | * handling for multi-dimensional arrays.
79 | *
80 | *
81 | * This method returns null if null array
82 | * input.
83 | *
84 | * @param array the array to shallow clone, may be null
85 | * @param the array type
86 | * @return the cloned array, null if null input
87 | */
88 | public static T[] shallowCopy(final T[] array) {
89 | if (array == null) {
90 | return null;
91 | }
92 | return array.clone();
93 | }
94 |
95 | /**
96 | *
97 | * Adds all the elements of the given arrays into a new array.
98 | *
99 | *
100 | * The new array contains all of the element of array1 followed
101 | * by all of the elements array2. When an array is returned, it
102 | * is always a new array.
103 | *
112 | *
113 | * @param array1 the first array whose elements are added to the new array,
114 | * may be null
115 | * @param array2 the second array whose elements are added to the new array,
116 | * may be null
117 | * @param the array type
118 | * @return The new array, null if null array
119 | * inputs. The type of the new array is the type of the first array.
120 | */
121 | public static T[] addAll(final T[] array1, final T[] array2) {
122 | if (array1 == null) {
123 | return shallowCopy(array2);
124 | } else if (array2 == null) {
125 | return shallowCopy(array1);
126 | }
127 | final T[] newArray = (T[]) Array.newInstance(array1.getClass().getComponentType(), array1.length + array2.length);
128 | System.arraycopy(array1, 0, newArray, 0, array1.length);
129 | System.arraycopy(array2, 0, newArray, array1.length, array2.length);
130 | return newArray;
131 | }
132 |
133 | public static T[] removeItem(T[] array, int pos) {
134 | int length = Array.getLength(array);
135 | T[] dest = (T[]) Array.newInstance(array.getClass().getComponentType(), length - 1);
136 |
137 | System.arraycopy(array, 0, dest, 0, pos);
138 | System.arraycopy(array, pos + 1, dest, pos, length - pos - 1);
139 | return dest;
140 | }
141 |
142 | public static T[] getArraySubset(T[] array, int start, int end) {
143 | return Arrays.copyOfRange(array, start, end);
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/com/wordlibrary/utils/ByteUtilities.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package com.wordlibrary.utils;
7 |
8 | /*
9 | * Copyright (c) Cedar Software, LLC
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License"); you
12 | * may not use this file except in compliance with the License. You may
13 | * obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | */
23 |
24 | public final class ByteUtilities
25 | {
26 | private static final char[] _hex =
27 | {
28 | '0', '1', '2', '3', '4', '5', '6', '7',
29 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
30 | };
31 |
32 |
33 | /**
34 | *
35 | * {@code StringUtilities} instances should NOT be constructed in standard
36 | * programming. Instead, the class should be used statically as
37 | * {@code StringUtilities.trim();}.
38 | *
39 | */
40 | private ByteUtilities() {
41 | super();
42 | }
43 |
44 | // Turn hex String into byte[]
45 | // If string is not even length, return null.
46 |
47 | public static byte[] decode(final String s)
48 | {
49 | int len = s.length();
50 | if (len % 2 != 0)
51 | {
52 | return null;
53 | }
54 |
55 | byte[] bytes = new byte[len / 2];
56 | int pos = 0;
57 |
58 | for (int i = 0; i < len; i += 2)
59 | {
60 | byte hi = (byte)Character.digit(s.charAt(i), 16);
61 | byte lo = (byte)Character.digit(s.charAt(i + 1), 16);
62 | bytes[pos++] = (byte)(hi * 16 + lo);
63 | }
64 |
65 | return bytes;
66 | }
67 |
68 | /**
69 | * Convert a byte array into a printable format containing a String of hex
70 | * digit characters (two per byte).
71 | *
72 | * @param bytes array representation
73 | * @return String hex digits
74 | */
75 | public static String encode(final byte[] bytes)
76 | {
77 | StringBuilder sb = new StringBuilder(bytes.length << 1);
78 | for (byte aByte : bytes)
79 | {
80 | sb.append(convertDigit(aByte >> 4));
81 | sb.append(convertDigit(aByte & 0x0f));
82 | }
83 | return sb.toString();
84 | }
85 |
86 | /**
87 | * Convert the specified value (0 .. 15) to the corresponding hex digit.
88 | *
89 | * @param value
90 | * to be converted
91 | * @return '0'..'F' in char format.
92 | */
93 | private static char convertDigit(final int value)
94 | {
95 | return _hex[value & 0x0f];
96 | }
97 |
98 | /**
99 | * @param bytes byte[] of bytes to test
100 | * @return true if bytes are gzip compressed, false otherwise.
101 | */
102 | public static boolean isGzipped(byte[] bytes)
103 | {
104 | return bytes[0] == (byte)0x1f && bytes[1] == (byte)0x8b;
105 | }
106 | }
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/com/wordlibrary/utils/DateUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.util.Calendar;
4 | import java.util.Date;
5 | import java.util.LinkedHashMap;
6 | import java.util.Map;
7 | import java.util.TimeZone;
8 | import java.util.regex.Matcher;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | * Handy utilities for working with Java Dates.
13 | *
14 | * @author John DeRegnaucourt (john@cedarsoftware.com)
15 | *
16 | * Copyright (c) Cedar Software LLC
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *
24 | * Unless required by applicable law or agreed to in writing, software
25 | * distributed under the License is distributed on an "AS IS" BASIS,
26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 | * See the License for the specific language governing permissions and
28 | * limitations under the License.
29 | */
30 | public final class DateUtilities
31 | {
32 | private static final String days = "(monday|mon|tuesday|tues|tue|wednesday|wed|thursday|thur|thu|friday|fri|saturday|sat|sunday|sun)"; // longer before shorter matters
33 | private static final String mos = "(Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|Sept|September|Oct|October|Nov|November|Dec|December)";
34 | private static final Pattern datePattern1 = Pattern.compile("(\\d{4})[./-](\\d{1,2})[./-](\\d{1,2})");
35 | private static final Pattern datePattern2 = Pattern.compile("(\\d{1,2})[./-](\\d{1,2})[./-](\\d{4})");
36 | private static final Pattern datePattern3 = Pattern.compile(mos + "[ ]*[,]?[ ]*(\\d{1,2})(st|nd|rd|th|)[ ]*[,]?[ ]*(\\d{4})", Pattern.CASE_INSENSITIVE);
37 | private static final Pattern datePattern4 = Pattern.compile("(\\d{1,2})(st|nd|rd|th|)[ ]*[,]?[ ]*" + mos + "[ ]*[,]?[ ]*(\\d{4})", Pattern.CASE_INSENSITIVE);
38 | private static final Pattern datePattern5 = Pattern.compile("(\\d{4})[ ]*[,]?[ ]*" + mos + "[ ]*[,]?[ ]*(\\d{1,2})(st|nd|rd|th|)", Pattern.CASE_INSENSITIVE);
39 | private static final Pattern datePattern6 = Pattern.compile(days+"[ ]+" + mos + "[ ]+(\\d{1,2})[ ]+(\\d{2}:\\d{2}:\\d{2})[ ]+[A-Z]{1,3}\\s+(\\d{4})", Pattern.CASE_INSENSITIVE);
40 | private static final Pattern timePattern1 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})[.](\\d{1,10})([+-]\\d{2}[:]?\\d{2}|Z)?");
41 | private static final Pattern timePattern2 = Pattern.compile("(\\d{2})[:.](\\d{2})[:.](\\d{2})([+-]\\d{2}[:]?\\d{2}|Z)?");
42 | private static final Pattern timePattern3 = Pattern.compile("(\\d{2})[:.](\\d{2})([+-]\\d{2}[:]?\\d{2}|Z)?");
43 | private static final Pattern dayPattern = Pattern.compile(days, Pattern.CASE_INSENSITIVE);
44 | private static final Map months = new LinkedHashMap<>();
45 |
46 | static
47 | {
48 | // Month name to number map
49 | months.put("jan", "1");
50 | months.put("january", "1");
51 | months.put("feb", "2");
52 | months.put("february", "2");
53 | months.put("mar", "3");
54 | months.put("march", "3");
55 | months.put("apr", "4");
56 | months.put("april", "4");
57 | months.put("may", "5");
58 | months.put("jun", "6");
59 | months.put("june", "6");
60 | months.put("jul", "7");
61 | months.put("july", "7");
62 | months.put("aug", "8");
63 | months.put("august", "8");
64 | months.put("sep", "9");
65 | months.put("sept", "9");
66 | months.put("september", "9");
67 | months.put("oct", "10");
68 | months.put("october", "10");
69 | months.put("nov", "11");
70 | months.put("november", "11");
71 | months.put("dec", "12");
72 | months.put("december", "12");
73 | }
74 |
75 | private DateUtilities() {
76 | super();
77 | }
78 |
79 | public static Date parseDate(String dateStr)
80 | {
81 | if (dateStr == null)
82 | {
83 | return null;
84 | }
85 | dateStr = dateStr.trim();
86 | if ("".equals(dateStr))
87 | {
88 | return null;
89 | }
90 |
91 | // Determine which date pattern (Matcher) to use
92 | Matcher matcher = datePattern1.matcher(dateStr);
93 |
94 | String year, month = null, day, mon = null, remains;
95 |
96 | if (matcher.find())
97 | {
98 | year = matcher.group(1);
99 | month = matcher.group(2);
100 | day = matcher.group(3);
101 | remains = matcher.replaceFirst("");
102 | }
103 | else
104 | {
105 | matcher = datePattern2.matcher(dateStr);
106 | if (matcher.find())
107 | {
108 | month = matcher.group(1);
109 | day = matcher.group(2);
110 | year = matcher.group(3);
111 | remains = matcher.replaceFirst("");
112 | }
113 | else
114 | {
115 | matcher = datePattern3.matcher(dateStr);
116 | if (matcher.find())
117 | {
118 | mon = matcher.group(1);
119 | day = matcher.group(2);
120 | year = matcher.group(4);
121 | remains = matcher.replaceFirst("");
122 | }
123 | else
124 | {
125 | matcher = datePattern4.matcher(dateStr);
126 | if (matcher.find())
127 | {
128 | day = matcher.group(1);
129 | mon = matcher.group(3);
130 | year = matcher.group(4);
131 | remains = matcher.replaceFirst("");
132 | }
133 | else
134 | {
135 | matcher = datePattern5.matcher(dateStr);
136 | if (matcher.find())
137 | {
138 | year = matcher.group(1);
139 | mon = matcher.group(2);
140 | day = matcher.group(3);
141 | remains = matcher.replaceFirst("");
142 | }
143 | else
144 | {
145 | matcher = datePattern6.matcher(dateStr);
146 | if (!matcher.find())
147 | {
148 | error("Unable to parse: " + dateStr);
149 | }
150 | year = matcher.group(5);
151 | mon = matcher.group(2);
152 | day = matcher.group(3);
153 | remains = matcher.group(4);
154 | }
155 | }
156 | }
157 | }
158 | }
159 |
160 | if (mon != null)
161 | { // Month will always be in Map, because regex forces this.
162 | month = months.get(mon.trim().toLowerCase());
163 | }
164 |
165 | // Determine which date pattern (Matcher) to use
166 | String hour = null, min = null, sec = "00", milli = "0", tz = null;
167 | remains = remains.trim();
168 | matcher = timePattern1.matcher(remains);
169 | if (matcher.find())
170 | {
171 | hour = matcher.group(1);
172 | min = matcher.group(2);
173 | sec = matcher.group(3);
174 | milli = matcher.group(4);
175 | if (matcher.groupCount() > 4)
176 | {
177 | tz = matcher.group(5);
178 | }
179 | }
180 | else
181 | {
182 | matcher = timePattern2.matcher(remains);
183 | if (matcher.find())
184 | {
185 | hour = matcher.group(1);
186 | min = matcher.group(2);
187 | sec = matcher.group(3);
188 | if (matcher.groupCount() > 3)
189 | {
190 | tz = matcher.group(4);
191 | }
192 | }
193 | else
194 | {
195 | matcher = timePattern3.matcher(remains);
196 | if (matcher.find())
197 | {
198 | hour = matcher.group(1);
199 | min = matcher.group(2);
200 | if (matcher.groupCount() > 2)
201 | {
202 | tz = matcher.group(3);
203 | }
204 | }
205 | else
206 | {
207 | matcher = null;
208 | }
209 | }
210 | }
211 |
212 | if (matcher != null)
213 | {
214 | remains = matcher.replaceFirst("");
215 | }
216 |
217 | // Clear out day of week (mon, tue, wed, ...)
218 | // if (StringUtilities.length(remains) > 0)
219 | // {
220 | // Matcher dayMatcher = dayPattern.matcher(remains);
221 | // if (dayMatcher.find())
222 | // {
223 | // remains = dayMatcher.replaceFirst("").trim();
224 | // }
225 | // }
226 | // if (StringUtilities.length(remains) > 0)
227 | // {
228 | // remains = remains.trim();
229 | // if (!remains.equals(",") && (!remains.equals("T")))
230 | // {
231 | // error("Issue parsing data/time, other characters present: " + remains);
232 | // }
233 | // }
234 |
235 | Calendar c = Calendar.getInstance();
236 | c.clear();
237 | if (tz != null)
238 | {
239 | if ("z".equalsIgnoreCase(tz))
240 | {
241 | c.setTimeZone(TimeZone.getTimeZone("GMT"));
242 | }
243 | else
244 | {
245 | c.setTimeZone(TimeZone.getTimeZone("GMT" + tz));
246 | }
247 | }
248 |
249 | // Regex prevents these from ever failing to parse
250 | int y = Integer.parseInt(year);
251 | int m = Integer.parseInt(month) - 1; // months are 0-based
252 | int d = Integer.parseInt(day);
253 |
254 | if (m < 0 || m > 11)
255 | {
256 | error("Month must be between 1 and 12 inclusive, date: " + dateStr);
257 | }
258 | if (d < 1 || d > 31)
259 | {
260 | error("Day must be between 1 and 31 inclusive, date: " + dateStr);
261 | }
262 |
263 | if (matcher == null)
264 | { // no [valid] time portion
265 | c.set(y, m, d);
266 | }
267 | else
268 | {
269 | // Regex prevents these from ever failing to parse.
270 | int h = Integer.parseInt(hour);
271 | int mn = Integer.parseInt(min);
272 | int s = Integer.parseInt(sec);
273 | int ms = Integer.parseInt(milli);
274 |
275 | if (h > 23)
276 | {
277 | error("Hour must be between 0 and 23 inclusive, time: " + dateStr);
278 | }
279 | if (mn > 59)
280 | {
281 | error("Minute must be between 0 and 59 inclusive, time: " + dateStr);
282 | }
283 | if (s > 59)
284 | {
285 | error("Second must be between 0 and 59 inclusive, time: " + dateStr);
286 | }
287 |
288 | // regex enforces millis to number
289 | c.set(y, m, d, h, mn, s);
290 | c.set(Calendar.MILLISECOND, ms);
291 | }
292 | return c.getTime();
293 | }
294 |
295 | private static void error(String msg)
296 | {
297 | throw new IllegalArgumentException(msg);
298 | }
299 | }
--------------------------------------------------------------------------------
/AnagramGame/src/com.wordlibrary/classes/module-info.java:
--------------------------------------------------------------------------------
1 | module com.wordlibrary {
2 | exports com.wordlibrary;
3 | exports com.wordlibrary.spi;
4 | uses com.wordlibrary.spi.WordLibraryProvider;
5 | }
6 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.animal/classes/module-info.java:
--------------------------------------------------------------------------------
1 | module org.wordlibrary.animal {
2 | requires com.wordlibrary;
3 | provides com.wordlibrary.spi.WordLibraryProvider
4 | with org.wordlibrary.animal.AnimalWordLibraryProvider;
5 | }
6 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.animal/classes/org/wordlibrary/animal/AnimalWordLibrary.java:
--------------------------------------------------------------------------------
1 | package org.wordlibrary.animal;
2 |
3 | import com.wordlibrary.WordLibrary;
4 |
5 | /**
6 | * Implementation of the logic for the Anagram Game application.
7 | */
8 | public final class AnimalWordLibrary extends WordLibrary {
9 |
10 | @Override
11 | public String toString() {
12 | return "Animal";
13 | }
14 |
15 | private static final String[] WORD_LIST = {
16 | "cat",
17 | "dog"};
18 |
19 | private static final String[] SCRAMBLED_WORD_LIST = {
20 | "tac",
21 | "odg"
22 | };
23 |
24 | /**
25 | * Singleton class.
26 | */
27 | public AnimalWordLibrary() {
28 | }
29 |
30 | /**
31 | * Gets the word at a given index.
32 | *
33 | * @param idx index of required word
34 | * @return word at that index in its natural form
35 | */
36 | public String getWord(int idx) {
37 | return WORD_LIST[idx];
38 | }
39 |
40 | /**
41 | * Gets the word at a given index in its scrambled form.
42 | *
43 | * @param idx index of required word
44 | * @return word at that index in its scrambled form
45 | */
46 | public String getScrambledWord(int idx) {
47 | return SCRAMBLED_WORD_LIST[idx];
48 | }
49 |
50 | /**
51 | * Gets the number of words in the library.
52 | *
53 | * @return the total number of plain/scrambled word pairs in the library
54 | */
55 | public int getSize() {
56 | return WORD_LIST.length;
57 | }
58 |
59 | /**
60 | * Checks whether a user's guess for a word at the given index is correct.
61 | *
62 | * @param idx index of the word guessed
63 | * @param userGuess the user's guess for the actual word
64 | * @return true if the guess was correct; false otherwise
65 | */
66 | public boolean isCorrect(int idx, String userGuess) {
67 | return userGuess.equals(getWord(idx));
68 | }
69 |
70 | }
71 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.animal/classes/org/wordlibrary/animal/AnimalWordLibraryProvider.java:
--------------------------------------------------------------------------------
1 | package org.wordlibrary.animal;
2 |
3 | import com.wordlibrary.WordLibrary;
4 | import com.wordlibrary.spi.WordLibraryProvider;
5 |
6 | public class AnimalWordLibraryProvider extends WordLibraryProvider {
7 |
8 | public AnimalWordLibraryProvider() {
9 | }
10 |
11 | @Override
12 | public WordLibrary openWordLibrary() {
13 | return new AnimalWordLibrary();
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.names/classes/module-info.java:
--------------------------------------------------------------------------------
1 |
2 | module org.wordlibrary.names {
3 | requires com.wordlibrary;
4 | provides com.wordlibrary.spi.WordLibraryProvider
5 | with org.wordlibrary.names.NameWordLibraryProvider;
6 | }
7 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.names/classes/org/wordlibrary/names/NameWordLibrary.java:
--------------------------------------------------------------------------------
1 | package org.wordlibrary.names;
2 |
3 | import com.wordlibrary.WordLibrary;
4 |
5 | /**
6 | * Implementation of the logic for the Anagram Game application.
7 | */
8 | public final class NameWordLibrary extends WordLibrary {
9 |
10 | @Override
11 | public String toString() {
12 | return "Names";
13 | }
14 |
15 | private static final String[] WORD_LIST = {
16 | "tom",
17 | "harry",
18 | "dick"};
19 |
20 | private static final String[] SCRAMBLED_WORD_LIST = {
21 | "mot",
22 | "yrrah",
23 | "kcid"
24 | };
25 |
26 | /**
27 | * Singleton class.
28 | */
29 | public NameWordLibrary() {
30 | }
31 |
32 | /**
33 | * Gets the word at a given index.
34 | * @param idx index of required word
35 | * @return word at that index in its natural form
36 | */
37 | public String getWord(int idx) {
38 | return WORD_LIST[idx];
39 | }
40 |
41 | /**
42 | * Gets the word at a given index in its scrambled form.
43 | * @param idx index of required word
44 | * @return word at that index in its scrambled form
45 | */
46 | public String getScrambledWord(int idx) {
47 | return SCRAMBLED_WORD_LIST[idx];
48 | }
49 |
50 | /**
51 | * Gets the number of words in the library.
52 | * @return the total number of plain/scrambled word pairs in the library
53 | */
54 | public int getSize() {
55 | return WORD_LIST.length;
56 | }
57 |
58 | /**
59 | * Checks whether a user's guess for a word at the given index is correct.
60 | * @param idx index of the word guessed
61 | * @param userGuess the user's guess for the actual word
62 | * @return true if the guess was correct; false otherwise
63 | */
64 | public boolean isCorrect(int idx, String userGuess) {
65 | return userGuess.equals(getWord(idx));
66 | }
67 |
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.names/classes/org/wordlibrary/names/NameWordLibraryProvider.java:
--------------------------------------------------------------------------------
1 | package org.wordlibrary.names;
2 |
3 | import com.wordlibrary.WordLibrary;
4 | import com.wordlibrary.spi.WordLibraryProvider;
5 |
6 | public class NameWordLibraryProvider extends WordLibraryProvider {
7 |
8 | public NameWordLibraryProvider() {
9 | }
10 |
11 | @Override
12 | public WordLibrary openWordLibrary() {
13 | return new NameWordLibrary();
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.technical/classes/module-info.java:
--------------------------------------------------------------------------------
1 | module org.wordlibrary.technical {
2 | requires com.wordlibrary;
3 | provides com.wordlibrary.spi.WordLibraryProvider
4 | with org.wordlibrary.technical.TechnicalWordLibraryProvider;
5 | }
6 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.technical/classes/org/wordlibrary/technical/TechnicalWordLibrary.java:
--------------------------------------------------------------------------------
1 | package org.wordlibrary.technical;
2 |
3 | import com.wordlibrary.WordLibrary;
4 |
5 | /**
6 | * Implementation of the logic for the Anagram Game application.
7 | */
8 | public final class TechnicalWordLibrary extends WordLibrary {
9 |
10 | @Override
11 | public String toString() {
12 | return "Technical";
13 | }
14 |
15 | private static final String[] WORD_LIST = {
16 | "abstraction",
17 | "ambiguous",
18 | "traditional"};
19 |
20 | private static final String[] SCRAMBLED_WORD_LIST = {
21 | "batsartcoin",
22 | "maibuguos",
23 | "rtdatioialn"
24 | };
25 |
26 | /**
27 | * Singleton class.
28 | */
29 | public TechnicalWordLibrary() {
30 | }
31 |
32 | /**
33 | * Gets the word at a given index.
34 | * @param idx index of required word
35 | * @return word at that index in its natural form
36 | */
37 | public String getWord(int idx) {
38 | return WORD_LIST[idx];
39 | }
40 |
41 | /**
42 | * Gets the word at a given index in its scrambled form.
43 | * @param idx index of required word
44 | * @return word at that index in its scrambled form
45 | */
46 | public String getScrambledWord(int idx) {
47 | return SCRAMBLED_WORD_LIST[idx];
48 | }
49 |
50 | /**
51 | * Gets the number of words in the library.
52 | * @return the total number of plain/scrambled word pairs in the library
53 | */
54 | public int getSize() {
55 | return WORD_LIST.length;
56 | }
57 |
58 | /**
59 | * Checks whether a user's guess for a word at the given index is correct.
60 | * @param idx index of the word guessed
61 | * @param userGuess the user's guess for the actual word
62 | * @return true if the guess was correct; false otherwise
63 | */
64 | public boolean isCorrect(int idx, String userGuess) {
65 | return userGuess.equals(getWord(idx));
66 | }
67 |
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/AnagramGame/src/org.wordlibrary.technical/classes/org/wordlibrary/technical/TechnicalWordLibraryProvider.java:
--------------------------------------------------------------------------------
1 | package org.wordlibrary.technical;
2 |
3 | import com.wordlibrary.WordLibrary;
4 | import com.wordlibrary.spi.WordLibraryProvider;
5 |
6 | public class TechnicalWordLibraryProvider extends WordLibraryProvider {
7 |
8 | public TechnicalWordLibraryProvider() {
9 | }
10 |
11 | @Override
12 | public WordLibrary openWordLibrary() {
13 | return new TechnicalWordLibrary();
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/AnagramGameMaven/README.md:
--------------------------------------------------------------------------------
1 | # Modular Maven Anagram Game
2 |
3 | A full example showing usage of --module-path in [nbactions.xml](https://github.com/GeertjanWielenga/JigsawJavaModularProjectSamples/blob/master/AnagramGameMaven/com.toy.anagram/nbactions.xml) of 'com.toy.anagram', consisting of three Jigsaw/Maven modules, i.e.,
4 | with module-info.java and POM.xml in each of the three projects, setting dependencies between them.
5 |
6 | The result is a Java Swing
7 | Anagram Game (com.toy.anagram) that loads services of com.wordlibrary, for example, org.wordlibrary.animal.
8 |
9 |
10 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/nbactions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | run
5 |
6 | jar
7 |
8 |
9 | process-classes
10 | org.codehaus.mojo:exec-maven-plugin:1.6.0:exec
11 |
12 |
13 | --module-path %classpath --module ${project.groupId}/${packageClassName}
14 | java
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | com.toy.anagram
5 | com.toy.anagram
6 | 1.0-SNAPSHOT
7 | jar
8 |
9 |
10 |
11 | org.apache.maven.plugins
12 | maven-compiler-plugin
13 | 3.8.0
14 |
15 | 11
16 | 11
17 | true
18 | true
19 |
20 |
21 |
22 |
23 |
24 |
25 | com.wordlibrary
26 | com.wordlibrary
27 | 1.0-SNAPSHOT
28 |
29 |
30 | org.wordlibrary
31 | org.wordlibrary.names
32 | 1.0-SNAPSHOT
33 |
34 |
35 | org.wordlibrary
36 | org.wordlibrary.animal
37 | 1.0-SNAPSHOT
38 |
39 |
40 |
41 | UTF-8
42 | 11
43 | 11
44 |
45 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/com/toy/anagram/ui/About.form:
--------------------------------------------------------------------------------
1 |
2 |
3 |
79 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/com/toy/anagram/ui/About.java:
--------------------------------------------------------------------------------
1 | package com.toy.anagram.ui;
2 |
3 | import java.awt.Dimension;
4 | import java.awt.Point;
5 | import java.awt.Rectangle;
6 | import java.awt.event.ActionListener;
7 | import javax.swing.JDialog;
8 | import javax.swing.JFrame;
9 |
10 | /**
11 | * About dialog of the Anagram Game application.
12 | */
13 | public class About extends JDialog {
14 |
15 | /** Creates new form About */
16 | public About(JFrame parent) {
17 | super(parent,true);
18 | initComponents();
19 | pack();
20 | Rectangle parentBounds = parent.getBounds();
21 | Dimension size = getSize();
22 | // Center in the parent
23 | int x = Math.max(0, parentBounds.x + (parentBounds.width - size.width) / 2);
24 | int y = Math.max(0, parentBounds.y + (parentBounds.height - size.height) / 2);
25 | setLocation(new Point(x, y));
26 | }
27 |
28 | /** This method is called from within the constructor to
29 | * initialize the form.
30 | * WARNING: Do NOT modify this code. The content of this method is
31 | * always regenerated by the Form Editor.
32 | */
33 | private void initComponents() {//GEN-BEGIN:initComponents
34 | java.awt.GridBagConstraints gridBagConstraints;
35 |
36 | mainPanel = new javax.swing.JPanel();
37 | copyrightTextArea = new javax.swing.JTextArea();
38 | closeButton = new javax.swing.JButton();
39 |
40 | getContentPane().setLayout(new java.awt.GridBagLayout());
41 |
42 | setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
43 | setTitle("About Anagrams");
44 | mainPanel.setLayout(new java.awt.GridBagLayout());
45 |
46 | mainPanel.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(11, 11, 12, 12)));
47 | copyrightTextArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Panel.background"));
48 | copyrightTextArea.setColumns(25);
49 | copyrightTextArea.setEditable(false);
50 | copyrightTextArea.setLineWrap(true);
51 | copyrightTextArea.setRows(8);
52 | copyrightTextArea.setText("Anagrams\n\nCopyright (c) 2003 Irritable Enterprises, Inc.");
53 | copyrightTextArea.setWrapStyleWord(true);
54 | copyrightTextArea.setBorder(null);
55 | copyrightTextArea.setFocusable(false);
56 | gridBagConstraints = new java.awt.GridBagConstraints();
57 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
58 | gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
59 | gridBagConstraints.weightx = 1.0;
60 | gridBagConstraints.weighty = 1.0;
61 | gridBagConstraints.insets = new java.awt.Insets(24, 0, 24, 0);
62 | mainPanel.add(copyrightTextArea, gridBagConstraints);
63 |
64 | closeButton.setMnemonic('C');
65 | closeButton.setText("Close");
66 | closeButton.addActionListener(new java.awt.event.ActionListener() {
67 | public void actionPerformed(java.awt.event.ActionEvent evt) {
68 | closeButtonActionPerformed(evt);
69 | }
70 | });
71 |
72 | gridBagConstraints = new java.awt.GridBagConstraints();
73 | gridBagConstraints.gridx = 0;
74 | gridBagConstraints.gridy = 1;
75 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
76 | mainPanel.add(closeButton, gridBagConstraints);
77 |
78 | gridBagConstraints = new java.awt.GridBagConstraints();
79 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
80 | gridBagConstraints.weightx = 1.0;
81 | gridBagConstraints.weighty = 1.0;
82 | getContentPane().add(mainPanel, gridBagConstraints);
83 |
84 | }//GEN-END:initComponents
85 |
86 | private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
87 | setVisible(false);
88 | dispose();
89 | }//GEN-LAST:event_closeButtonActionPerformed
90 |
91 |
92 | // Variables declaration - do not modify//GEN-BEGIN:variables
93 | private javax.swing.JButton closeButton;
94 | private javax.swing.JTextArea copyrightTextArea;
95 | private javax.swing.JPanel mainPanel;
96 | // End of variables declaration//GEN-END:variables
97 |
98 | }
99 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/com/toy/anagram/ui/Anagrams.form:
--------------------------------------------------------------------------------
1 |
2 |
3 |
211 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/com/toy/anagram/ui/Anagrams.java:
--------------------------------------------------------------------------------
1 |
2 | /* Anagram Game Application */
3 |
4 | package com.toy.anagram.ui;
5 |
6 | import com.wordlibrary.WordLibrary;
7 | import java.awt.Dimension;
8 | import java.awt.Point;
9 | import java.awt.Toolkit;
10 | import java.util.List;
11 | import javax.swing.DefaultComboBoxModel;
12 | import javax.swing.JFrame;
13 | import javax.swing.SwingUtilities;
14 |
15 | /**
16 | * Main window of the Anagram Game application.
17 | */
18 | public class Anagrams extends JFrame {
19 |
20 | public static void main(String[] args) {
21 | /* Set the Nimbus look and feel */
22 | //
23 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
24 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
25 | */
26 | try {
27 | javax.swing.UIManager.LookAndFeelInfo[] installedLookAndFeels=javax.swing.UIManager.getInstalledLookAndFeels();
28 | for (int idx=0; idx
43 | //
44 | //
45 | //
46 |
47 | /* Create and display the form */
48 | SwingUtilities.invokeLater(new Runnable() {
49 | public void run() {
50 | new Anagrams().setVisible(true);
51 | }
52 | });
53 | }
54 |
55 | private int wordIdx = 0;
56 |
57 | private DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
58 |
59 |
60 | // private WordLibrary wordLibrary;
61 |
62 | /** Creates new form Anagrams */
63 | public Anagrams() {
64 |
65 | List libraries = WordLibrary.openWordLibraries();
66 |
67 | initComponents();
68 |
69 | for (WordLibrary library : libraries) {
70 | dcbm.addElement(library);
71 | }
72 |
73 | jComboBox1.setModel(dcbm);
74 |
75 | getRootPane().setDefaultButton(guessButton);
76 | pack();
77 | guessedWord.requestFocusInWindow();
78 | // Center in the screen
79 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
80 | Dimension frameSize = getSize();
81 | setLocation(new Point((screenSize.width - frameSize.width) / 2,
82 | (screenSize.height - frameSize.width) / 2));
83 | }
84 |
85 | /** This method is called from within the constructor to
86 | * initialize the form.
87 | * WARNING: Do NOT modify this code. The content of this method is
88 | * always regenerated by the Form Editor.
89 | */
90 | // //GEN-BEGIN:initComponents
91 | private void initComponents() {
92 | java.awt.GridBagConstraints gridBagConstraints;
93 |
94 | mainPanel = new javax.swing.JPanel();
95 | scrambledLabel = new javax.swing.JLabel();
96 | scrambledWord = new javax.swing.JTextField();
97 | guessLabel = new javax.swing.JLabel();
98 | guessedWord = new javax.swing.JTextField();
99 | feedbackLabel = new javax.swing.JLabel();
100 | buttonsPanel = new javax.swing.JPanel();
101 | guessButton = new javax.swing.JButton();
102 | nextTrial = new javax.swing.JButton();
103 | jComboBox1 = new javax.swing.JComboBox<>();
104 | jLabel1 = new javax.swing.JLabel();
105 | mainMenu = new javax.swing.JMenuBar();
106 | fileMenu = new javax.swing.JMenu();
107 | aboutMenuItem = new javax.swing.JMenuItem();
108 | exitMenuItem = new javax.swing.JMenuItem();
109 |
110 | setTitle("Anagrams");
111 | addWindowListener(new java.awt.event.WindowAdapter() {
112 | public void windowClosing(java.awt.event.WindowEvent evt) {
113 | exitForm(evt);
114 | }
115 | });
116 |
117 | mainPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 12, 12));
118 | mainPanel.setMinimumSize(new java.awt.Dimension(297, 200));
119 | mainPanel.setLayout(new java.awt.GridBagLayout());
120 |
121 | scrambledLabel.setText("Scrambled Word:");
122 | gridBagConstraints = new java.awt.GridBagConstraints();
123 | gridBagConstraints.gridx = 0;
124 | gridBagConstraints.gridy = 1;
125 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
126 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
127 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
128 | mainPanel.add(scrambledLabel, gridBagConstraints);
129 |
130 | scrambledWord.setEditable(false);
131 | scrambledWord.setColumns(20);
132 | gridBagConstraints = new java.awt.GridBagConstraints();
133 | gridBagConstraints.gridx = 1;
134 | gridBagConstraints.gridy = 1;
135 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
136 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
137 | gridBagConstraints.weightx = 1.0;
138 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
139 | mainPanel.add(scrambledWord, gridBagConstraints);
140 |
141 | guessLabel.setDisplayedMnemonic('Y');
142 | guessLabel.setLabelFor(guessedWord);
143 | guessLabel.setText("Your Guess:");
144 | gridBagConstraints = new java.awt.GridBagConstraints();
145 | gridBagConstraints.gridx = 0;
146 | gridBagConstraints.gridy = 2;
147 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
148 | gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
149 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 6);
150 | mainPanel.add(guessLabel, gridBagConstraints);
151 |
152 | guessedWord.setColumns(20);
153 | gridBagConstraints = new java.awt.GridBagConstraints();
154 | gridBagConstraints.gridx = 1;
155 | gridBagConstraints.gridy = 2;
156 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
157 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
158 | gridBagConstraints.weightx = 1.0;
159 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
160 | mainPanel.add(guessedWord, gridBagConstraints);
161 |
162 | feedbackLabel.setText(" ");
163 | gridBagConstraints = new java.awt.GridBagConstraints();
164 | gridBagConstraints.gridx = 1;
165 | gridBagConstraints.gridy = 3;
166 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
167 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168 | gridBagConstraints.weightx = 1.0;
169 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 20, 0);
170 | mainPanel.add(feedbackLabel, gridBagConstraints);
171 |
172 | buttonsPanel.setLayout(new java.awt.GridBagLayout());
173 |
174 | guessButton.setMnemonic('G');
175 | guessButton.setText("Guess");
176 | guessButton.setToolTipText("Guess the scrambled word.");
177 | guessButton.addActionListener(new java.awt.event.ActionListener() {
178 | public void actionPerformed(java.awt.event.ActionEvent evt) {
179 | guessedWordActionPerformed(evt);
180 | }
181 | });
182 | gridBagConstraints = new java.awt.GridBagConstraints();
183 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
184 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
185 | gridBagConstraints.weightx = 1.0;
186 | gridBagConstraints.weighty = 1.0;
187 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 6);
188 | buttonsPanel.add(guessButton, gridBagConstraints);
189 |
190 | nextTrial.setMnemonic('N');
191 | nextTrial.setText("New Word");
192 | nextTrial.setToolTipText("Fetch a new word.");
193 | nextTrial.addActionListener(new java.awt.event.ActionListener() {
194 | public void actionPerformed(java.awt.event.ActionEvent evt) {
195 | nextTrialActionPerformed(evt);
196 | }
197 | });
198 | gridBagConstraints = new java.awt.GridBagConstraints();
199 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
200 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
201 | gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTHEAST;
202 | gridBagConstraints.weighty = 1.0;
203 | buttonsPanel.add(nextTrial, gridBagConstraints);
204 |
205 | gridBagConstraints = new java.awt.GridBagConstraints();
206 | gridBagConstraints.gridx = 0;
207 | gridBagConstraints.gridy = 4;
208 | gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
209 | gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
210 | gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
211 | gridBagConstraints.weighty = 1.0;
212 | mainPanel.add(buttonsPanel, gridBagConstraints);
213 |
214 | jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
215 | gridBagConstraints = new java.awt.GridBagConstraints();
216 | gridBagConstraints.gridx = 1;
217 | gridBagConstraints.gridy = 0;
218 | gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219 | gridBagConstraints.ipady = 6;
220 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
221 | mainPanel.add(jComboBox1, gridBagConstraints);
222 |
223 | jLabel1.setText("Category:");
224 | gridBagConstraints = new java.awt.GridBagConstraints();
225 | gridBagConstraints.gridx = 0;
226 | gridBagConstraints.gridy = 0;
227 | gridBagConstraints.ipady = 6;
228 | gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
229 | gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 6);
230 | mainPanel.add(jLabel1, gridBagConstraints);
231 |
232 | getContentPane().add(mainPanel, java.awt.BorderLayout.CENTER);
233 |
234 | fileMenu.setMnemonic('F');
235 | fileMenu.setText("File");
236 |
237 | aboutMenuItem.setMnemonic('A');
238 | aboutMenuItem.setText("About");
239 | aboutMenuItem.setToolTipText("About");
240 | aboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
241 | public void actionPerformed(java.awt.event.ActionEvent evt) {
242 | aboutMenuItemActionPerformed(evt);
243 | }
244 | });
245 | fileMenu.add(aboutMenuItem);
246 |
247 | exitMenuItem.setMnemonic('E');
248 | exitMenuItem.setText("Exit");
249 | exitMenuItem.setToolTipText("Quit Team, Quit!");
250 | exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
251 | public void actionPerformed(java.awt.event.ActionEvent evt) {
252 | exitMenuItemActionPerformed(evt);
253 | }
254 | });
255 | fileMenu.add(exitMenuItem);
256 |
257 | mainMenu.add(fileMenu);
258 |
259 | setJMenuBar(mainMenu);
260 | }// //GEN-END:initComponents
261 |
262 | private void aboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutMenuItemActionPerformed
263 | new About(this).setVisible(true);
264 | }//GEN-LAST:event_aboutMenuItemActionPerformed
265 |
266 | private void nextTrialActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextTrialActionPerformed
267 |
268 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
269 |
270 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
271 |
272 | wordIdx = (wordIdx + 1) % wordLibrary.getSize();
273 |
274 | feedbackLabel.setText(" ");
275 | scrambledWord.setText(wordLibrary.getScrambledWord(wordIdx));
276 | guessedWord.setText("");
277 | getRootPane().setDefaultButton(guessButton);
278 |
279 | guessedWord.requestFocusInWindow();
280 | }//GEN-LAST:event_nextTrialActionPerformed
281 |
282 | private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
283 | System.exit(0);
284 | }//GEN-LAST:event_exitMenuItemActionPerformed
285 |
286 | private void guessedWordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guessedWordActionPerformed
287 | WordLibrary wordLibrary = (WordLibrary) dcbm.getSelectedItem();
288 |
289 | if (wordLibrary.isCorrect(wordIdx, guessedWord.getText())){
290 | feedbackLabel.setText("Correct! Try a new word!");
291 | getRootPane().setDefaultButton(nextTrial);
292 | } else {
293 | feedbackLabel.setText("Incorrect! Try again!");
294 | guessedWord.setText("");
295 | }
296 |
297 | guessedWord.requestFocusInWindow();
298 | }//GEN-LAST:event_guessedWordActionPerformed
299 |
300 | private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
301 | System.exit(0);
302 | }//GEN-LAST:event_exitForm
303 |
304 | // Variables declaration - do not modify//GEN-BEGIN:variables
305 | private javax.swing.JMenuItem aboutMenuItem;
306 | private javax.swing.JPanel buttonsPanel;
307 | private javax.swing.JMenuItem exitMenuItem;
308 | private javax.swing.JLabel feedbackLabel;
309 | private javax.swing.JMenu fileMenu;
310 | private javax.swing.JButton guessButton;
311 | private javax.swing.JLabel guessLabel;
312 | private javax.swing.JTextField guessedWord;
313 | private javax.swing.JComboBox jComboBox1;
314 | private javax.swing.JLabel jLabel1;
315 | private javax.swing.JMenuBar mainMenu;
316 | private javax.swing.JPanel mainPanel;
317 | private javax.swing.JButton nextTrial;
318 | private javax.swing.JLabel scrambledLabel;
319 | private javax.swing.JTextField scrambledWord;
320 | // End of variables declaration//GEN-END:variables
321 |
322 | }
323 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.toy.anagram/src/main/java/module-info.java:
--------------------------------------------------------------------------------
1 | module com.toy.anagram {
2 | requires java.base;
3 | requires java.desktop;
4 | requires java.logging;
5 | requires com.wordlibrary;
6 | }
7 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 | com.wordlibrary
5 | com.wordlibrary
6 | 1.0-SNAPSHOT
7 | jar
8 |
9 |
10 |
11 | org.apache.maven.plugins
12 | maven-compiler-plugin
13 | 3.6.1
14 |
15 |
16 |
17 |
18 | UTF-8
19 | 11
20 | 11
21 |
22 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/WordLibrary.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary;
2 |
3 | import com.wordlibrary.spi.WordLibraryProvider;
4 | import java.util.ArrayList;
5 | import java.util.Iterator;
6 | import java.util.List;
7 | import java.util.ServiceLoader;
8 |
9 | public abstract class WordLibrary {
10 |
11 | /**
12 | * Constructor for subclasses.
13 | */
14 | protected WordLibrary() {
15 | }
16 |
17 | static List wordLibraryList = new ArrayList();
18 |
19 | public static List openWordLibraries() {
20 | ServiceLoader sl
21 | = ServiceLoader.load(WordLibraryProvider.class);
22 | Iterator iter = sl.iterator();
23 | if (!iter.hasNext()) {
24 | throw new RuntimeException("No service providers found!");
25 | }
26 | while (iter.hasNext()) {
27 | WordLibraryProvider provider = iter.next();
28 | wordLibraryList.add(provider.openWordLibrary());
29 | }
30 | return wordLibraryList;
31 | }
32 |
33 | /**
34 | * Gets the word at a given index.
35 | *
36 | * @param idx index of required word
37 | * @return word at that index in its natural form
38 | */
39 | public abstract String getWord(int idx);
40 |
41 | /**
42 | * Gets the word at a given index in its scrambled form.
43 | *
44 | * @param idx index of required word
45 | * @return word at that index in its scrambled form
46 | */
47 | public abstract String getScrambledWord(int idx);
48 |
49 | /**
50 | * Gets the number of words in the library.
51 | *
52 | * @return the total number of plain/scrambled word pairs in the library
53 | */
54 | public abstract int getSize();
55 |
56 | /**
57 | * Checks whether a user's guess for a word at the given index is correct.
58 | *
59 | * @param idx index of the word guessed
60 | * @param userGuess the user's guess for the actual word
61 | * @return true if the guess was correct; false otherwise
62 | */
63 | public abstract boolean isCorrect(int idx, String userGuess);
64 | }
65 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/spi/WordLibraryProvider.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.spi;
2 |
3 | import com.wordlibrary.WordLibrary;
4 |
5 | public abstract class WordLibraryProvider {
6 |
7 | protected WordLibraryProvider() {
8 | }
9 |
10 | public abstract WordLibrary openWordLibrary();
11 |
12 | }
13 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/utils/ArrayUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.lang.reflect.Array;
4 | import java.util.Arrays;
5 |
6 | /**
7 | * Handy utilities for working with Java arrays.
8 | *
9 | * @author Ken Partlow
10 | * @author John DeRegnaucourt (john@cedarsoftware.com)
11 | *
12 | * Copyright (c) Cedar Software LLC
13 | *
14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not
15 | * use this file except in compliance with the License. You may obtain a copy of
16 | * the License at
17 | *
20 | * Unless required by applicable law or agreed to in writing, software
21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
23 | * License for the specific language governing permissions and limitations under
24 | * the License.
25 | */
26 | public final class ArrayUtilities {
27 |
28 | /**
29 | * Immutable common arrays.
30 | */
31 | public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
32 | public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
33 |
34 | /**
35 | * Private constructor to promote using as static class.
36 | */
37 | private ArrayUtilities() {
38 | super();
39 | }
40 |
41 | /**
42 | * This is a null-safe isEmpty check. It uses the Array static class for
43 | * doing a length check. This check is actually .0001 ms slower than the
44 | * following typed check:
45 | *
48 | * but gives you more flexibility, since it checks for all array types.
49 | *
50 | * @param array array to check
51 | * @return true if empty or null
52 | */
53 | public static boolean isEmpty(final Object array) {
54 | return array == null || Array.getLength(array) == 0;
55 | }
56 |
57 | /**
58 | * This is a null-safe size check. It uses the Array static class for doing
59 | * a length check. This check is actually .0001 ms slower than the following
60 | * typed check:
61 | *
77 | * The objects in the array are not cloned, thus there is no special
78 | * handling for multi-dimensional arrays.
79 | *
80 | *
81 | * This method returns null if null array
82 | * input.
83 | *
84 | * @param array the array to shallow clone, may be null
85 | * @param the array type
86 | * @return the cloned array, null if null input
87 | */
88 | public static T[] shallowCopy(final T[] array) {
89 | if (array == null) {
90 | return null;
91 | }
92 | return array.clone();
93 | }
94 |
95 | /**
96 | *
97 | * Adds all the elements of the given arrays into a new array.
98 | *
99 | *
100 | * The new array contains all of the element of array1 followed
101 | * by all of the elements array2. When an array is returned, it
102 | * is always a new array.
103 | *
112 | *
113 | * @param array1 the first array whose elements are added to the new array,
114 | * may be null
115 | * @param array2 the second array whose elements are added to the new array,
116 | * may be null
117 | * @param the array type
118 | * @return The new array, null if null array
119 | * inputs. The type of the new array is the type of the first array.
120 | */
121 | public static T[] addAll(final T[] array1, final T[] array2) {
122 | if (array1 == null) {
123 | return shallowCopy(array2);
124 | } else if (array2 == null) {
125 | return shallowCopy(array1);
126 | }
127 | final T[] newArray = (T[]) Array.newInstance(array1.getClass().getComponentType(), array1.length + array2.length);
128 | System.arraycopy(array1, 0, newArray, 0, array1.length);
129 | System.arraycopy(array2, 0, newArray, array1.length, array2.length);
130 | return newArray;
131 | }
132 |
133 | public static T[] removeItem(T[] array, int pos) {
134 | int length = Array.getLength(array);
135 | T[] dest = (T[]) Array.newInstance(array.getClass().getComponentType(), length - 1);
136 |
137 | System.arraycopy(array, 0, dest, 0, pos);
138 | System.arraycopy(array, pos + 1, dest, pos, length - pos - 1);
139 | return dest;
140 | }
141 |
142 | public static T[] getArraySubset(T[] array, int start, int end) {
143 | return Arrays.copyOfRange(array, start, end);
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/utils/ByteUtilities.java:
--------------------------------------------------------------------------------
1 | /*
2 | * To change this license header, choose License Headers in Project Properties.
3 | * To change this template file, choose Tools | Templates
4 | * and open the template in the editor.
5 | */
6 | package com.wordlibrary.utils;
7 |
8 | /*
9 | * Copyright (c) Cedar Software, LLC
10 | *
11 | * Licensed under the Apache License, Version 2.0 (the "License"); you
12 | * may not use this file except in compliance with the License. You may
13 | * obtain a copy of the License at
14 | *
15 | * http://www.apache.org/licenses/LICENSE-2.0
16 | *
17 | * Unless required by applicable law or agreed to in writing, software
18 | * distributed under the License is distributed on an "AS IS" BASIS,
19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 | * See the License for the specific language governing permissions and
21 | * limitations under the License.
22 | */
23 |
24 | public final class ByteUtilities
25 | {
26 | private static final char[] _hex =
27 | {
28 | '0', '1', '2', '3', '4', '5', '6', '7',
29 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
30 | };
31 |
32 |
33 | /**
34 | *
35 | * {@code StringUtilities} instances should NOT be constructed in standard
36 | * programming. Instead, the class should be used statically as
37 | * {@code StringUtilities.trim();}.
38 | *
39 | */
40 | private ByteUtilities() {
41 | super();
42 | }
43 |
44 | // Turn hex String into byte[]
45 | // If string is not even length, return null.
46 |
47 | public static byte[] decode(final String s)
48 | {
49 | int len = s.length();
50 | if (len % 2 != 0)
51 | {
52 | return null;
53 | }
54 |
55 | byte[] bytes = new byte[len / 2];
56 | int pos = 0;
57 |
58 | for (int i = 0; i < len; i += 2)
59 | {
60 | byte hi = (byte)Character.digit(s.charAt(i), 16);
61 | byte lo = (byte)Character.digit(s.charAt(i + 1), 16);
62 | bytes[pos++] = (byte)(hi * 16 + lo);
63 | }
64 |
65 | return bytes;
66 | }
67 |
68 | /**
69 | * Convert a byte array into a printable format containing a String of hex
70 | * digit characters (two per byte).
71 | *
72 | * @param bytes array representation
73 | * @return String hex digits
74 | */
75 | public static String encode(final byte[] bytes)
76 | {
77 | StringBuilder sb = new StringBuilder(bytes.length << 1);
78 | for (byte aByte : bytes)
79 | {
80 | sb.append(convertDigit(aByte >> 4));
81 | sb.append(convertDigit(aByte & 0x0f));
82 | }
83 | return sb.toString();
84 | }
85 |
86 | /**
87 | * Convert the specified value (0 .. 15) to the corresponding hex digit.
88 | *
89 | * @param value
90 | * to be converted
91 | * @return '0'..'F' in char format.
92 | */
93 | private static char convertDigit(final int value)
94 | {
95 | return _hex[value & 0x0f];
96 | }
97 |
98 | /**
99 | * @param bytes byte[] of bytes to test
100 | * @return true if bytes are gzip compressed, false otherwise.
101 | */
102 | public static boolean isGzipped(byte[] bytes)
103 | {
104 | return bytes[0] == (byte)0x1f && bytes[1] == (byte)0x8b;
105 | }
106 | }
--------------------------------------------------------------------------------
/AnagramGameMaven/com.wordlibrary/src/main/java/com/wordlibrary/utils/DateUtilities.java:
--------------------------------------------------------------------------------
1 | package com.wordlibrary.utils;
2 |
3 | import java.util.Calendar;
4 | import java.util.Date;
5 | import java.util.LinkedHashMap;
6 | import java.util.Map;
7 | import java.util.TimeZone;
8 | import java.util.regex.Matcher;
9 | import java.util.regex.Pattern;
10 |
11 | /**
12 | * Handy utilities for working with Java Dates.
13 | *
14 | * @author John DeRegnaucourt (john@cedarsoftware.com)
15 | *
16 | * Copyright (c) Cedar Software LLC
17 | *
18 | * Licensed under the Apache License, Version 2.0 (the "License");
19 | * you may not use this file except in compliance with the License.
20 | * You may obtain a copy of the License at
21 | *