1. Dmytro Kovalchuk
  2. cross-stitch

Commits

Dmytro Kovalchuk  committed d3f4d41 Draft

Code quality session

  • Participants
  • Parent commits 7af99d8
  • Branches default
  • Tags build-4

Comments (0)

Files changed (11)

File base-palettes/src/main/java/net/anatolich/cstitch/palette/file/PaletteTextFileParser.java

View file
     @Override
     protected List<PaletteColor> doParse(BufferedReader reader) throws IOException {
         try {
-            List<PaletteColor> colors = new ArrayList<PaletteColor>();
+            List<PaletteColor> colors = new ArrayList<>();
 
             String line = reader.readLine();
             while (line != null) {
     }
 
     private String getNextNonEmptyLine(String line, BufferedReader reader) throws IOException {
-        while (line != null && line.trim().isEmpty()) {
-            line = reader.readLine();
+        String nextLine = line;
+        while (nextLine != null && nextLine.trim().isEmpty()) {
+            nextLine = reader.readLine();
         }
-        if(line != null){
-            line = line.trim();
+        if(nextLine != null){
+            nextLine = nextLine.trim();
         }
-        return line;
+        return nextLine;
     }
 
     protected Color parseColor(String colorRgb) {

File cross-stitch-api/src/main/java/net/anatolich/cstitch/palette/api/PaletteColor.java

View file
 import java.awt.Color;
 import java.util.Collections;
 import java.util.List;
+import net.anatolich.cstitch.util.EqualUtil;
 
 /**
  * Named color from palette.
 
         final PaletteColor other = (PaletteColor) obj;
 
-        if ((this.name == null) ? (other.name != null)
-                : !this.name.equals(other.name)) {
+        if (!EqualUtil.nullAwareEqual(this.name, other.name)) {
             return false;
         }
 
-        if ((this.color != other.color) && ((this.color == null) || !this.color.equals(other.color))) {
+        if (!EqualUtil.nullAwareEqual(this.color, other.color)) {
             return false;
         }
 
-        if ((this.palette != other.palette) && ((this.palette == null) || !this.palette.equals(other.palette))) {
+        if (!EqualUtil.nullAwareEqual(this.palette, other.palette)) {
             return false;
         }
 

File cross-stitch-api/src/main/java/net/anatolich/cstitch/schema/DefaultLegend.java

View file
  */
 public class DefaultLegend implements Legend {
 
-    private final List<PaletteColor> colorList = new ArrayList<PaletteColor>();
+    private final List<PaletteColor> colorList = new ArrayList<>();
     private PaletteColor backgroundColor = new PaletteColor("Background", Color.WHITE);
-    private LinkedList<LegendChangeListener> listeners = new LinkedList<LegendChangeListener>();
+    private List<LegendChangeListener> listeners = new LinkedList<>();
     private PaletteColor selectedColor;
 
     public DefaultLegend() {

File cross-stitch-api/src/main/java/net/anatolich/cstitch/util/EqualUtil.java

View file
+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.cstitch.util;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public class EqualUtil {
+
+    private EqualUtil() {
+    }
+
+    public static boolean nullAwareEqual(Object o1, Object o2) {
+        if (o1 == null && o2 == null) {
+            return true;
+        }
+
+        if (o1 == null || o2 == null) {
+            return false;
+        }
+        
+        return o1.equals(o2);
+    }
+}

File cross-stitch-api/src/test/java/net/anatolich/cstitch/palette/api/PaletteColorTest.java

View file
+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.cstitch.palette.api;
+
+import java.awt.Color;
+import static org.easymock.EasyMock.createMock;
+import static org.junit.Assert.*;
+import static org.hamcrest.CoreMatchers.*;
+import org.junit.Test;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public class PaletteColorTest {
+    
+    @Test(expected=NullPointerException.class)
+    public void checkConstructorValidation(){
+        PaletteColor paletteColor = new PaletteColor(null, Color.BLACK);
+        
+        assertNull(paletteColor);
+    }
+    
+    @Test(expected=NullPointerException.class)
+    public void checkSecondConstructorValidation(){
+        PaletteColor paletteColor = new PaletteColor(null, Color.BLACK, createMock(Palette.class));
+        
+        assertNull(paletteColor);
+    }
+    
+    @Test
+    public void testBuilderWithoutPalette(){
+        final Color color = Color.CYAN;
+        final String name = "CYAN";
+        PaletteColor expected = new PaletteColor(name, color);
+        
+        PaletteColor.Builder builder = new PaletteColor.Builder();
+        PaletteColor paletteColor = builder.setColor(color).setName(name).build();
+        
+        assertEquals(expected, paletteColor);
+    }
+    
+    @Test
+    public void testBuilderWithPalette(){
+        final Color color = Color.CYAN;
+        final String name = "CYAN";
+        Palette palette = createMock(Palette.class);
+        PaletteColor expected = new PaletteColor(name, color, palette);
+        
+        PaletteColor.Builder builder = new PaletteColor.Builder();
+        PaletteColor paletteColor = builder.setColor(color).setName(name).setPalette(palette).build();
+        
+        assertEquals(expected, paletteColor);
+    }
+    
+    @Test
+    public void testEquality(){
+        final Color color = Color.CYAN;
+        final String name = "CYAN";
+        Palette palette = createMock(Palette.class);
+        PaletteColor oneColor = new PaletteColor(name, color, palette);
+        
+        PaletteColor.Builder builder = new PaletteColor.Builder();
+        PaletteColor otherColor = builder.setColor(color).setName(name).build();         
+        
+        assertThat("Colors of different palettes must not be equal", oneColor, not(equalTo(otherColor)));
+    }
+}

File cross-stitch-api/src/test/java/net/anatolich/cstitch/schema/CoordinatesTest.java

View file
+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.cstitch.schema;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public class CoordinatesTest {
+
+    @Test
+    public void testEquality() {
+        Coordinates a = new Coordinates(1, 1);
+        Coordinates sameAsA = new Coordinates(1, 1);
+        Coordinates other = new Coordinates(1, 2);
+
+        assertEquals(a, sameAsA);
+        assertEquals(sameAsA, a);
+
+        assertThat(other, not(equalTo(a)));
+        assertThat(a, not(equalTo(other)));
+    }
+
+    @Test
+    public void testMoveToDirection() {
+        Coordinates base = new Coordinates(8, 4);
+        Coordinates east2 = new Coordinates(10, 4);
+        Coordinates west3 = new Coordinates(5, 4);
+        Coordinates north1 = new Coordinates(8, 3);
+        Coordinates south4 = new Coordinates(8, 8);
+
+        assertEquals("East 2", east2, base.byDirection(Coordinates.Direction.EAST, 2));
+        assertEquals("West 3", west3, base.byDirection(Coordinates.Direction.WEST, 3));
+        assertEquals("North 1", north1, base.byDirection(Coordinates.Direction.NORTH, 1));
+        assertEquals("North 1", north1, base.byDirection(Coordinates.Direction.NORTH));
+        assertEquals("South 4", south4, base.byDirection(Coordinates.Direction.SOUTH, 4));
+    }
+
+    @Test
+    public void testConstructorSetup() {
+        int x = 5;
+        int y = 10;
+        Coordinates coordinates = new Coordinates(x, y);
+
+        assertEquals("X don't match", x, coordinates.getX());
+        assertEquals("Y don't match", y, coordinates.getY());
+    }
+}

File cross-stitch-api/src/test/java/net/anatolich/cstitch/util/EqualUtilTest.java

View file
+/*
+ * Copyright 2012 Anatolich <anatolich@anatolich.net>.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package net.anatolich.cstitch.util;
+
+import org.junit.AfterClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.junit.BeforeClass;
+
+/**
+ *
+ * @author Anatolich <anatolich@anatolich.net>
+ */
+public class EqualUtilTest {
+
+    private String one = "One";
+    private String same = "One";
+    private String other = "Other";
+    private String cornerCase = null;
+    Integer otherClass = 42;
+
+
+    @Test
+    public void testObjectAndNullReversed() {
+        assertFalse(EqualUtil.nullAwareEqual(cornerCase, one));
+    }
+
+    @Test
+    public void testObjectAndNull() {
+        assertFalse(EqualUtil.nullAwareEqual(one, cornerCase));
+    }
+
+    @Test
+    public void testObjectsOfDifferentClasses() {
+        assertFalse(EqualUtil.nullAwareEqual(one, otherClass));
+    }
+
+    @Test
+    public void testReversedDifferentObject() {
+        assertFalse(EqualUtil.nullAwareEqual(other, one));
+    }
+
+    @Test
+    public void testDifferendObjects() {
+        assertFalse(EqualUtil.nullAwareEqual(one, other));
+    }
+
+    @Test
+    public void testReverseEqualObjects() {
+        assertTrue(EqualUtil.nullAwareEqual(same, one));
+    }
+
+    @Test
+    public void testEqualObjects() {
+        assertTrue(EqualUtil.nullAwareEqual(one, same));
+    }
+
+    @Test
+    public void testSameObjectEquality() {
+        assertTrue(EqualUtil.nullAwareEqual(one, one));
+    }
+
+    @Test
+    public void testNullsEquality() {
+        assertTrue(EqualUtil.nullAwareEqual(cornerCase, cornerCase));
+    }
+}

File swing-api-impl/src/main/java/net/anatolich/cstitch/swing/tools/impl/Pencil.java

View file
  */
 package net.anatolich.cstitch.swing.tools.impl;
 
-import java.awt.event.InputEvent;
 import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
-import javax.swing.AbstractAction;
-import static javax.swing.Action.*;
 import javax.swing.ImageIcon;
 import javax.swing.KeyStroke;
 import net.anatolich.cstitch.palette.api.PaletteColor;
 import net.anatolich.cstitch.schema.Canvas;
 import net.anatolich.cstitch.schema.Legend;
 import net.anatolich.cstitch.ui.editor.tools.AbstractSwingTool;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Paints one cross with selected color on left click. Erases to background
  */
 public final class Pencil extends AbstractSwingTool {
 
-    private static final Logger log = LoggerFactory.getLogger(Pencil.class);
-
     @Override
     public void mousePressed(MouseEvent e) {
         if (e.getButton() == MouseEvent.BUTTON2) {

File swing-api/src/main/java/net/anatolich/cstitch/ui/editor/legend/JLegendPanelBeanInfo.java

-/*
- * Copyright 2012 dmytro.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package net.anatolich.cstitch.ui.editor.legend;
-
-import java.beans.*;
-
-/**
- *
- * @author dmytro
- */
-public class JLegendPanelBeanInfo extends SimpleBeanInfo {
-
-    // Bean descriptor//GEN-FIRST:BeanDescriptor
-    /*lazy BeanDescriptor*/
-    private static BeanDescriptor getBdescriptor(){
-        BeanDescriptor beanDescriptor = new BeanDescriptor  ( net.anatolich.cstitch.ui.editor.legend.JLegendPanel.class , null ); // NOI18N//GEN-HEADEREND:BeanDescriptor
-
-        // Here you can add code for customizing the BeanDescriptor.
-
-        return beanDescriptor;     }//GEN-LAST:BeanDescriptor
-    // Property identifiers//GEN-FIRST:Properties
-    private static final int PROPERTY_legend = 0;
-
-    // Property array 
-    /*lazy PropertyDescriptor*/
-    private static PropertyDescriptor[] getPdescriptor(){
-        PropertyDescriptor[] properties = new PropertyDescriptor[1];
-    
-        try {
-            properties[PROPERTY_legend] = new PropertyDescriptor ( "legend", net.anatolich.cstitch.ui.editor.legend.JLegendPanel.class, "getLegend", "setLegend" ); // NOI18N
-            properties[PROPERTY_legend].setBound ( true );
-        }
-        catch(IntrospectionException e) {
-            e.printStackTrace();
-        }//GEN-HEADEREND:Properties
-
-        // Here you can add code for customizing the properties array.
-
-        return properties;     }//GEN-LAST:Properties
-    // EventSet identifiers//GEN-FIRST:Events
-
-    // EventSet array
-    /*lazy EventSetDescriptor*/
-    private static EventSetDescriptor[] getEdescriptor(){
-        EventSetDescriptor[] eventSets = new EventSetDescriptor[0];//GEN-HEADEREND:Events
-
-        // Here you can add code for customizing the event sets array.
-
-        return eventSets;     }//GEN-LAST:Events
-    // Method identifiers//GEN-FIRST:Methods
-
-    // Method array 
-    /*lazy MethodDescriptor*/
-    private static MethodDescriptor[] getMdescriptor(){
-        MethodDescriptor[] methods = new MethodDescriptor[0];//GEN-HEADEREND:Methods
-
-        // Here you can add code for customizing the methods array.
-        
-        return methods;     }//GEN-LAST:Methods
-    private static java.awt.Image iconColor16 = null;//GEN-BEGIN:IconsDef
-    private static java.awt.Image iconColor32 = null;
-    private static java.awt.Image iconMono16 = null;
-    private static java.awt.Image iconMono32 = null;//GEN-END:IconsDef
-    private static String iconNameC16 = null;//GEN-BEGIN:Icons
-    private static String iconNameC32 = null;
-    private static String iconNameM16 = null;
-    private static String iconNameM32 = null;//GEN-END:Icons
-    private static final int defaultPropertyIndex = -1;//GEN-BEGIN:Idx
-    private static final int defaultEventIndex = -1;//GEN-END:Idx
-
-//GEN-FIRST:Superclass
-    // Here you can add code for customizing the Superclass BeanInfo.
-//GEN-LAST:Superclass
-    /**
-     * Gets the bean's
-     * <code>BeanDescriptor</code>s.
-     *
-     * @return BeanDescriptor describing the editable properties of this bean.
-     * May return null if the information should be obtained by automatic
-     * analysis.
-     */
-    public BeanDescriptor getBeanDescriptor() {
-        return getBdescriptor();
-    }
-
-    /**
-     * Gets the bean's
-     * <code>PropertyDescriptor</code>s.
-     *
-     * @return An array of PropertyDescriptors describing the editable
-     * properties supported by this bean. May return null if the information
-     * should be obtained by automatic analysis. <p> If a property is indexed,
-     * then its entry in the result array will belong to the
-     * IndexedPropertyDescriptor subclass of PropertyDescriptor. A client of
-     * getPropertyDescriptors can use "instanceof" to check if a given
-     * PropertyDescriptor is an IndexedPropertyDescriptor.
-     */
-    public PropertyDescriptor[] getPropertyDescriptors() {
-        return getPdescriptor();
-    }
-
-    /**
-     * Gets the bean's
-     * <code>EventSetDescriptor</code>s.
-     *
-     * @return An array of EventSetDescriptors describing the kinds of events
-     * fired by this bean. May return null if the information should be obtained
-     * by automatic analysis.
-     */
-    public EventSetDescriptor[] getEventSetDescriptors() {
-        return getEdescriptor();
-    }
-
-    /**
-     * Gets the bean's
-     * <code>MethodDescriptor</code>s.
-     *
-     * @return An array of MethodDescriptors describing the methods implemented
-     * by this bean. May return null if the information should be obtained by
-     * automatic analysis.
-     */
-    public MethodDescriptor[] getMethodDescriptors() {
-        return getMdescriptor();
-    }
-
-    /**
-     * A bean may have a "default" property that is the property that will
-     * mostly commonly be initially chosen for update by human's who are
-     * customizing the bean.
-     *
-     * @return Index of default property in the PropertyDescriptor array
-     * returned by getPropertyDescriptors. <P>	Returns -1 if there is no default
-     * property.
-     */
-    public int getDefaultPropertyIndex() {
-        return defaultPropertyIndex;
-    }
-
-    /**
-     * A bean may have a "default" event that is the event that will mostly
-     * commonly be used by human's when using the bean.
-     *
-     * @return Index of default event in the EventSetDescriptor array returned
-     * by getEventSetDescriptors. <P>	Returns -1 if there is no default event.
-     */
-    public int getDefaultEventIndex() {
-        return defaultEventIndex;
-    }
-
-    /**
-     * This method returns an image object that can be used to represent the
-     * bean in toolboxes, toolbars, etc. Icon images will typically be GIFs, but
-     * may in future include other formats. <p> Beans aren't required to provide
-     * icons and may return null from this method. <p> There are four possible
-     * flavors of icons (16x16 color, 32x32 color, 16x16 mono, 32x32 mono). If a
-     * bean choses to only support a single icon we recommend supporting 16x16
-     * color. <p> We recommend that icons have a "transparent" background so
-     * they can be rendered onto an existing background.
-     *
-     * @param iconKind The kind of icon requested. This should be one of the
-     * constant values ICON_COLOR_16x16, ICON_COLOR_32x32, ICON_MONO_16x16, or
-     * ICON_MONO_32x32.
-     * @return An image object representing the requested icon. May return null
-     * if no suitable icon is available.
-     */
-    public java.awt.Image getIcon(int iconKind) {
-        switch (iconKind) {
-            case ICON_COLOR_16x16:
-                if (iconNameC16 == null) {
-                    return null;
-                } else {
-                    if (iconColor16 == null) {
-                        iconColor16 = loadImage(iconNameC16);
-                    }
-                    return iconColor16;
-                }
-            case ICON_COLOR_32x32:
-                if (iconNameC32 == null) {
-                    return null;
-                } else {
-                    if (iconColor32 == null) {
-                        iconColor32 = loadImage(iconNameC32);
-                    }
-                    return iconColor32;
-                }
-            case ICON_MONO_16x16:
-                if (iconNameM16 == null) {
-                    return null;
-                } else {
-                    if (iconMono16 == null) {
-                        iconMono16 = loadImage(iconNameM16);
-                    }
-                    return iconMono16;
-                }
-            case ICON_MONO_32x32:
-                if (iconNameM32 == null) {
-                    return null;
-                } else {
-                    if (iconMono32 == null) {
-                        iconMono32 = loadImage(iconNameM32);
-                    }
-                    return iconMono32;
-                }
-            default:
-                return null;
-        }
-    }
-}

File swing-api/src/main/java/net/anatolich/cstitch/ui/editor/tools/AbstractSwingTool.java

View file
             throw new NullPointerException("editor cannot be null");
         }
 
-        if ((editor == prevEditor) && (toolAction != null)) {
+        if ((editor.equals(prevEditor)) && (toolAction != null)) {
             return toolAction;
         }
 

File swing-api/src/main/java/net/anatolich/cstitch/ui/validation/UIValidator.java

View file
  */
 public class UIValidator {
 
+    private UIValidator() {
+    }
+
     /**
      * Validates integer which must be more than 0.
+     *
      * @param widthText
-     * @return 
+     * @return
      */
     public static boolean validatePositiveInteger(String widthText) {
         if (widthText != null && !widthText.isEmpty()) {