From 54901f700576f862dd65a5bc5806bae09802fd30 Mon Sep 17 00:00:00 2001 From: Neil McErlean Date: Wed, 19 Nov 2008 15:39:07 +0000 Subject: [PATCH] Initial impl of Forms config git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@11993 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../org/alfresco/web/config/StringPair.java | 100 ++++++++++++++++++ .../alfresco/web/config/StringPairTest.java | 84 +++++++++++++++ 2 files changed, 184 insertions(+) create mode 100644 source/java/org/alfresco/web/config/StringPair.java create mode 100644 source/java/org/alfresco/web/config/StringPairTest.java diff --git a/source/java/org/alfresco/web/config/StringPair.java b/source/java/org/alfresco/web/config/StringPair.java new file mode 100644 index 0000000000..01dbf45c04 --- /dev/null +++ b/source/java/org/alfresco/web/config/StringPair.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2005-2008 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.config; + +/** + * This immutable class represents a pair of Strings, such as a Name-Value pair. A null + * value is allowed, but a null name is not. + * + * @author Neil McErlean. + */ +public class StringPair implements Comparable +{ + private final String name; + private final String value; + + public StringPair(String name, String value) + { + this.name = name; + this.value = value; + } + + public String getName() + { + return this.name; + } + + public String getValue() + { + return this.value; + } + + public int compareTo(StringPair otherStringPair) + { + if (otherStringPair == null) + { + throw new NullPointerException("Cannot compareTo null."); + } + if (this.equals(otherStringPair)) + { + return 0; + } + return this.toString().compareTo(otherStringPair.toString()); + } + + @Override + public boolean equals(Object otherObj) + { + if (otherObj == null || !otherObj.getClass().equals(this.getClass())) + { + return false; + } + StringPair otherStringPair = (StringPair)otherObj; + + // These String.valueOf calls will protect us from NPEs in the case of + // null values. + return this.name.equals(otherStringPair.name) + && String.valueOf(this.value).equals(String.valueOf(otherStringPair.value)); + } + + @Override + public int hashCode() + { + int valueHashCode = 0; + if (value != null) + { + valueHashCode = value.hashCode(); + } + return name.hashCode() + 7 * valueHashCode; + } + + @Override + public String toString() + { + StringBuilder result = new StringBuilder(); + result.append(name).append("=").append(value); + return result.toString(); + } +} diff --git a/source/java/org/alfresco/web/config/StringPairTest.java b/source/java/org/alfresco/web/config/StringPairTest.java new file mode 100644 index 0000000000..e4d95a54c0 --- /dev/null +++ b/source/java/org/alfresco/web/config/StringPairTest.java @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2005-2008 Alfresco Software Limited. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + * As a special exception to the terms and conditions of version 2.0 of + * the GPL, you may redistribute this Program in connection with Free/Libre + * and Open Source Software ("FLOSS") applications as described in Alfresco's + * FLOSS exception. You should have received a copy of the text describing + * the FLOSS exception, and it is also available here: + * http://www.alfresco.com/legal/licensing" + */ +package org.alfresco.web.config; + +import junit.framework.TestCase; + +public class StringPairTest extends TestCase +{ + private final StringPair pairA = new StringPair("name", "Fred"); + private final StringPair pairAduplicate = new StringPair("name", "Fred"); + private final StringPair pairB = new StringPair("name", "Barney"); + + public StringPairTest(String name) + { + super(name); + } + + public void testNullValuedPair() + { + StringPair nullValue = new StringPair("NullValue", null); + + // These calls are to generate potential NPEs. + int ignored = nullValue.hashCode(); + boolean dontCare = nullValue.equals(pairA); + dontCare = pairA.equals(nullValue); + ignored = nullValue.compareTo(pairA); + ignored = pairA.compareTo(nullValue); + } + + public void testHashCode() + { + assertEquals("Equal objects must have equal hashCodes.", pairA.hashCode(), pairAduplicate.hashCode()); + } + + public void testEquals() + { + assertEquals("Equal objects appear unequal.", pairA, pairAduplicate); + assertTrue("Unequal objects appear equal.", !pairA.equals(pairB)); + } + + public void testCompareTo() + { + assertEquals("Equal objects have compareTo == 0.", 0, pairA.compareTo(pairAduplicate)); + assertEquals("Equal objects have compareTo == 0.", 0, pairAduplicate.compareTo(pairA)); + + assertTrue("Unequal objects should have compareTo != 0.", pairA.compareTo(pairB) > 0); + assertTrue("Unequal objects should have compareTo != 0.", pairB.compareTo(pairA) < 0); + } + + public void testCompareToNull() + { + try + { + pairA.compareTo(null); + } + catch (NullPointerException expected) + { + return; + } + fail("Expected NPE not thrown."); + } +}