From 8fd6f32e5fa26f5e7971d4c64112903469f76d4d Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Wed, 3 Jun 2015 17:23:30 +0300 Subject: [PATCH 1/5] Implement equality for StaticCluster --- .../clustering/algo/StaticCluster.java | 15 ++++++ .../android/clustering/StaticClusterTest.java | 48 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java diff --git a/library/src/com/google/maps/android/clustering/algo/StaticCluster.java b/library/src/com/google/maps/android/clustering/algo/StaticCluster.java index 265ce95da..be013969a 100644 --- a/library/src/com/google/maps/android/clustering/algo/StaticCluster.java +++ b/library/src/com/google/maps/android/clustering/algo/StaticCluster.java @@ -49,4 +49,19 @@ public String toString() { ", mItems.size=" + mItems.size() + '}'; } + + @Override + public int hashCode() { + return mCenter.hashCode() + mItems.hashCode(); + }; + + @Override + public boolean equals(Object other) { + if (!(other instanceof StaticCluster)) { + return false; + } + + return ((StaticCluster) other).mCenter.equals(mCenter) + && ((StaticCluster) other).mItems.equals(mItems); + } } \ No newline at end of file diff --git a/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java b/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java new file mode 100644 index 000000000..0c2b0a00f --- /dev/null +++ b/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2015 Stefanos Togoulidis, all rights reserved. + * + * 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 com.google.maps.android.clustering; + +import com.google.android.gms.maps.model.LatLng; +import com.google.maps.android.clustering.algo.StaticCluster; + +import junit.framework.TestCase; + +public class StaticClusterTest extends TestCase { + + private StaticCluster mCluster; + + public void setUp() { + mCluster = new StaticCluster(new LatLng(0.1, 0.5)); + } + + public void testEquality() { + StaticCluster cluster_1_5 = new StaticCluster( + new LatLng(0.1, 0.5)); + + assertEquals(cluster_1_5, mCluster); + assertNotSame(cluster_1_5, mCluster); + assertEquals(cluster_1_5.hashCode(), mCluster.hashCode()); + } + + public void testUnequality() { + StaticCluster cluster_2_3 = new StaticCluster( + new LatLng(0.2, 0.3)); + + assertFalse(mCluster.equals(cluster_2_3)); + assertFalse(cluster_2_3.hashCode() == mCluster.hashCode()); + } +} From c5d495a0ef12f9f4298f5cb896f483cc0f77abe2 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Wed, 3 Jun 2015 18:30:05 +0300 Subject: [PATCH 2/5] Implement equality for QuadItem --- .../NonHierarchicalDistanceBasedAlgorithm.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java b/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java index 06597262f..c01b7a0ca 100644 --- a/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java +++ b/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java @@ -183,5 +183,19 @@ public Set getItems() { public int getSize() { return 1; } + + @Override + public int hashCode() { + return mClusterItem.hashCode(); + }; + + @Override + public boolean equals(Object other) { + if (!(other instanceof QuadItem)) { + return false; + } + + return ((QuadItem) other).mClusterItem.equals(mClusterItem); + } } } From f49f604ee1262105a40ca61bcae90f217f22043c Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Wed, 3 Jun 2015 18:33:30 +0300 Subject: [PATCH 3/5] Implement NonHierarchicalDistanceBasedAlgorithm::removeItem --- ...NonHierarchicalDistanceBasedAlgorithm.java | 9 ++- .../maps/android/clustering/QuadItemTest.java | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 library/tests/src/com/google/maps/android/clustering/QuadItemTest.java diff --git a/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java b/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java index c01b7a0ca..f9b851c72 100644 --- a/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java +++ b/library/src/com/google/maps/android/clustering/algo/NonHierarchicalDistanceBasedAlgorithm.java @@ -71,8 +71,13 @@ public void clearItems() { @Override public void removeItem(T item) { - // TODO: delegate QuadItem#hashCode and QuadItem#equals to its item. - throw new UnsupportedOperationException("NonHierarchicalDistanceBasedAlgorithm.remove not implemented"); + // QuadItem delegates hashcode() and equals() to its item so, + // removing any QuadItem to that item will remove the item + final QuadItem quadItem = new QuadItem(item); + synchronized (mQuadTree) { + mItems.remove(quadItem); + mQuadTree.remove(quadItem); + } } @Override diff --git a/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java b/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java new file mode 100644 index 000000000..e786d4471 --- /dev/null +++ b/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java @@ -0,0 +1,61 @@ +/* + * Copyright 2015 Stefanos Togoulidis, all rights reserved. + * + * 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 com.google.maps.android.clustering; + +import com.google.android.gms.maps.model.LatLng; +import com.google.maps.android.clustering.algo.NonHierarchicalDistanceBasedAlgorithm; + +import junit.framework.TestCase; + +public class QuadItemTest extends TestCase { + + public class TestingItem implements ClusterItem { + private final LatLng mPosition; + + public TestingItem(double lat, double lng) { + mPosition = new LatLng(lat, lng); + } + + @Override + public LatLng getPosition() { + return mPosition; + } + } + + public void setUp() { + // nothing to setup + } + + public void testRemoval() { + TestingItem item_1_5 = new TestingItem(0.1, 0.5); + TestingItem item_2_3 = new TestingItem(0.2, 0.3); + + NonHierarchicalDistanceBasedAlgorithm algo + = new NonHierarchicalDistanceBasedAlgorithm(); + algo.addItem(item_1_5); + algo.addItem(item_2_3); + + assertEquals(2, algo.getItems().size()); + + algo.removeItem(item_1_5); + + assertEquals(1, algo.getItems().size()); + + assertFalse(algo.getItems().contains(item_1_5)); + assertTrue(algo.getItems().contains(item_2_3)); + } +} From 15f895bd569f0be50eb43b9ce06771aee2152254 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 19 Feb 2016 10:46:18 +0200 Subject: [PATCH 4/5] Set copyright to Google Inc. --- .../src/com/google/maps/android/clustering/QuadItemTest.java | 2 +- .../com/google/maps/android/clustering/StaticClusterTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java b/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java index e786d4471..2f20240ba 100644 --- a/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java +++ b/library/tests/src/com/google/maps/android/clustering/QuadItemTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Stefanos Togoulidis, all rights reserved. + * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java b/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java index 0c2b0a00f..3efa3ba56 100644 --- a/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java +++ b/library/tests/src/com/google/maps/android/clustering/StaticClusterTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Stefanos Togoulidis, all rights reserved. + * Copyright 2015 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From e64c52a5462d9abd492434c1cf2594f3424a8a96 Mon Sep 17 00:00:00 2001 From: Stefanos Togkoulidis Date: Fri, 19 Feb 2016 10:46:34 +0200 Subject: [PATCH 5/5] Add self to CONTRIBUTORS.md list --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 3311824df..1b885c544 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -9,3 +9,4 @@ * Mihai Preda, https://github.com/preda * Iris Uy, https://github.com/irisu * Emma Yeap, https://github.com/microcat +* Stefanos Togoulidis, https://github.com/hypest