Revision: 1874
Author: [email protected]
Date: Thu Mar 25 06:55:51 2010
Log: Comparator to compare IDoapResources based on name (related to Issue
25)
http://code.google.com/p/simal/source/detail?r=1874
Added:
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/utils
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/utils/DoapResourceByNameComparator.java
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/DoapResourceForTest.java
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/utils
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/utils/IDoapResourceByNameComparatorTest.java
=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.core/src/main/java/uk/ac/osswatch/simal/model/utils/DoapResourceByNameComparator.java
Thu Mar 25 06:55:51 2010
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2010 University of Oxford
+ *
+ * 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 uk.ac.osswatch.simal.model.utils;
+
+import java.util.Comparator;
+
+import uk.ac.osswatch.simal.model.IDoapResource;
+
+/**
+ * Comparator to compare IDoapResources by their default names
+ */
+public class DoapResourceByNameComparator implements
Comparator<IDoapResource> {
+
+ /**
+ * Compare IDoapResources by their default names
+ *
+ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
+ */
+ public int compare(IDoapResource a, IDoapResource b) {
+ int result = (a == null ?
+ (b == null ? 0 : -1) :
+ (b == null ? 1 : compareNames(a, b)));
+
+ return result;
+ }
+
+ /**
+ * Compares two IDoapResources by name assuming that both objects
+ * are non-null.
+ *
+ * @param a
+ * @param b
+ * @return
+ */
+ private int compareNames(IDoapResource a, IDoapResource b) {
+ String aName = a.getName();
+ String bName = b.getName();
+
+ int result = (aName == null ?
+ (bName == null ? 0 : -1) :
+ (bName == null ? 1 : aName.compareTo(bName)));
+
+ return result;
+ }
+}
=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/DoapResourceForTest.java
Thu Mar 25 06:55:51 2010
@@ -0,0 +1,366 @@
+/*
+ * Copyright 2010 University of Oxford
+ *
+ * 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 uk.ac.osswatch.simal.model;
+
+import java.net.URI;
+import java.net.URL;
+import java.util.Set;
+
+import uk.ac.osswatch.simal.rdf.SimalRepositoryException;
+
+/**
+ * Test class for testing IDoapResources that have nothing to do with the
Jena
+ * back-end. Expand when needed.
+ */
+public class DoapResourceForTest implements IDoapResource {
+
+ private static final long serialVersionUID = 8162751003605007841L;
+
+ private String name;
+
+ /**
+ * Return the name as set
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getName()
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * Setter for testing purposes.
+ * @param name
+ */
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
uk.ac.osswatch.simal.model.IDoapResource#addName(java.lang.String)
+ */
+ public void addName(String name) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getCreated()
+ */
+ public String getCreated() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getDescription()
+ */
+ public String getDescription() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getLabel()
+ */
+ public String getLabel() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getLicences()
+ */
+ public Set<IDoapLicence> getLicences() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getNames()
+ */
+ public Set<String> getNames() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#getShortDesc()
+ */
+ public String getShortDesc() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
uk.ac.osswatch.simal.model.IDoapResource#removeName(java.lang.String)
+ */
+ public void removeName(String name) throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
uk.ac.osswatch.simal.model.IDoapResource#setCreated(java.lang.String)
+ */
+ public void setCreated(String newCreated) throws
SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ *
uk.ac.osswatch.simal.model.IDoapResource#setDescription(java.lang.String)
+ */
+ public void setDescription(String newDescription) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ *
uk.ac.osswatch.simal.model.IDoapResource#setShortDesc(java.lang.String)
+ */
+ public void setShortDesc(String shortDesc) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#toJSON()
+ */
+ public String toJSON() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IDoapResource#toJSONRecord()
+ */
+ public String toJSONRecord() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#addSeeAlso(java.net.URI)
+ */
+ public void addSeeAlso(URI uri) {
+ // Not currently needed
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getComment()
+ */
+ public String getComment() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getLabel(java.lang.String)
+ */
+ public String getLabel(String defaultLabel) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getRepositoryResource()
+ */
+ public Object getRepositoryResource() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getSeeAlso()
+ */
+ public Set<URI> getSeeAlso() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getSimalID()
+ */
+ public String getSimalID() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getURI()
+ */
+ public String getURI() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getURL()
+ */
+ public URL getURL() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#getUniqueSimalID()
+ */
+ public String getUniqueSimalID() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#setComment(java.lang.String)
+ */
+ public void setComment(String comment) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#setLabel(java.lang.String)
+ */
+ public void setLabel(String label) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#setSeeAlso(java.util.Set)
+ */
+ public void setSeeAlso(Set<URI> seeAlso) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#setSimalID(java.lang.String)
+ */
+ public void setSimalID(String newID) throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResource#setURI(java.lang.String)
+ */
+ public void setURI(String uri) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResourceService#delete()
+ */
+ public void delete() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResourceService#getSources()
+ */
+ public Set<String> getSources() {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResourceService#toJSON(boolean)
+ */
+ public String toJSON(boolean asRecord) {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see uk.ac.osswatch.simal.model.IResourceService#toXML()
+ */
+ public String toXML() throws SimalRepositoryException {
+ // Not currently needed
+ throw new UnsupportedOperationException();
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/uk.ac.osswatch.simal.core/src/test/java/uk/ac/osswatch/simal/model/utils/IDoapResourceByNameComparatorTest.java
Thu Mar 25 06:55:51 2010
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2010 University of Oxford
+ *
+ * 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 uk.ac.osswatch.simal.model.utils;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+
+import org.junit.Test;
+
+import uk.ac.osswatch.simal.model.DoapResourceForTest;
+
+/**
+ * Test whether the DoapResourceByNameComparator compares correctly.
+ */
+public class IDoapResourceByNameComparatorTest {
+
+ private static final DoapResourceByNameComparator comp = new
DoapResourceByNameComparator();
+
+ @Test
+ public void testIDoapResourcesByName() {
+
+ DoapResourceForTest emptyName = new DoapResourceForTest();
+ DoapResourceForTest t1 = new DoapResourceForTest();
+ DoapResourceForTest t2 = new DoapResourceForTest();
+ t1.setName("aaa");
+ t2.setName("zzz");
+
+ assertEquals(0, comp.compare(null, null));
+ assertEquals(0, comp.compare(t1, t1));
+ assertEquals(0, comp.compare(t2, t2));
+ assertEquals(0, comp.compare(emptyName, emptyName));
+
+ assertTrue(comp.compare(t1, t2) < 0);
+ assertTrue(comp.compare(t2, t1) > 0);
+ assertTrue(comp.compare(emptyName, t1) < 0);
+ assertTrue(comp.compare(emptyName, t2) < 0);
+
+ assertTrue(comp.compare(null, t2) < 0);
+ assertTrue(comp.compare(t2, null) > 0);
+ assertTrue(comp.compare(null, emptyName) < 0);
+ assertTrue(comp.compare(emptyName, null) > 0);
+ }
+
+
+}
--
You received this message because you are subscribed to the Google Groups "Simal
Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/simal-commits?hl=en.