From d1b1e180d6e058d3e463462bc796ea5f32c47cb3 Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Thu, 24 Jul 2014 18:20:21 +0100 Subject: [PATCH] HCOLL-119 removed the use of " public SharedHashMap create(File file, Class kClass, Class vClass) " --- .../openhft/collections/CollectionView.java | 80 +++++++++++++++++++ .../ConcurrentMapLamdadSupport.java | 2 +- .../net/openhft/collections/KeySetView.java | 3 +- .../openhft/collections/VanillaSharedMap.java | 11 ++- .../chronicle/map/ChronicleMap8Test.java | 64 +++++++-------- 5 files changed, 123 insertions(+), 37 deletions(-) create mode 100644 src/main/java/net/openhft/collections/CollectionView.java diff --git a/src/main/java/net/openhft/collections/CollectionView.java b/src/main/java/net/openhft/collections/CollectionView.java new file mode 100644 index 000000000..2e2f10c32 --- /dev/null +++ b/src/main/java/net/openhft/collections/CollectionView.java @@ -0,0 +1,80 @@ +/* + * Copyright 2014 Higher Frequency Trading + *

+ * http://www.higherfrequencytrading.com + *

+ * 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.openhft.collections; + +import java.util.Collection; +import java.util.Iterator; + +/** + * @author Rob Austin. + */ +public interface CollectionView { + + AbstractVanillaSharedMap getMap(); + + /** + * Removes all of the elements from this view, by removing all the mappings from the map backing this + * view. + */ + void clear(); + + int size(); + + + boolean isEmpty(); + + // implementations below rely on concrete classes supplying these + // abstract methods + + /** + * Returns an iterator over the elements in this collection. + * + *

The returned iterator is weakly consistent. + * + * @return an iterator over the elements in this collection + */ + Iterator iterator(); + + boolean contains(Object o); + + boolean remove(Object o); + + static final String oomeMsg = "Required array size too large"; + + Object[] toArray(); + + @SuppressWarnings("unchecked") + T[] toArray(T[] a); + + /** + * Returns a string representation of this collection. The string representation consists of the string + * representations of the collection's elements in the order they are returned by its iterator, enclosed + * in square brackets ({@code "[]"}). Adjacent elements are separated by the characters {@code ", "} + * (comma and space). Elements are converted to strings as by {@link String#valueOf(Object)}. + * + * @return a string representation of this collection + */ + String toString(); + + boolean containsAll(Collection c); + + boolean removeAll(Collection c); + + boolean retainAll(Collection c); +} diff --git a/src/main/java/net/openhft/collections/ConcurrentMapLamdadSupport.java b/src/main/java/net/openhft/collections/ConcurrentMapLamdadSupport.java index 8567e5023..ea31db616 100644 --- a/src/main/java/net/openhft/collections/ConcurrentMapLamdadSupport.java +++ b/src/main/java/net/openhft/collections/ConcurrentMapLamdadSupport.java @@ -42,7 +42,7 @@ public interface ConcurrentMapLamdadSupport { * * @return the set view */ - AbstractVanillaSharedMap.KeySetView keySet(); + net.openhft.collections.KeySetView keySet(); long mappingCount(); diff --git a/src/main/java/net/openhft/collections/KeySetView.java b/src/main/java/net/openhft/collections/KeySetView.java index 74d49bfe9..589e8dbb4 100644 --- a/src/main/java/net/openhft/collections/KeySetView.java +++ b/src/main/java/net/openhft/collections/KeySetView.java @@ -27,7 +27,8 @@ import java.util.function.Consumer; /** * @author Rob Austin. */ -public interface KeySetView extends Collection, java.io.Serializable, Set { +public interface KeySetView extends Collection, java.io.Serializable, Set, CollectionView { /** * Returns the default mapped value for additions, or {@code null} if additions are not supported. * diff --git a/src/main/java/net/openhft/collections/VanillaSharedMap.java b/src/main/java/net/openhft/collections/VanillaSharedMap.java index 165991d2b..65a0784ec 100644 --- a/src/main/java/net/openhft/collections/VanillaSharedMap.java +++ b/src/main/java/net/openhft/collections/VanillaSharedMap.java @@ -84,7 +84,7 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co * Base class for views. */ abstract static class CollectionView - implements Collection, java.io.Serializable { + implements Collection, net.openhft.collections.CollectionView, java.io.Serializable { private static final long serialVersionUID = 7249069246763182397L; final AbstractVanillaSharedMap map; @@ -452,6 +452,7 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co return new KeySetView(this, mappedValue); } + /** * A view of a ConcurrentHashMap as a {@link Set} of keys, in which additions may optionally be enabled by * mapping to a common value. This class cannot be directly instantiated. See {@link #keySet() keySet()}, @@ -462,6 +463,7 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co */ public static class KeySetView extends CollectionView implements Set, java.io.Serializable, net.openhft.collections.KeySetView { + private static final long serialVersionUID = 7249069246763182397L; private final V value; @@ -1639,7 +1641,11 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co return putVal(key, value, false); } - V putVal(K key, V value, boolean replaceIfPresent) { + V putVal(K key, V value, boolean onlyIfAbsent) { + return put0(key, value, !onlyIfAbsent); + } + + V put0(K key, V value, boolean replaceIfPresent) { super.putVal(key, value, replaceIfPresent); checkKey(key); checkValue(value); @@ -2731,7 +2737,6 @@ abstract class AbstractVanillaSharedMap extends net.openhft.collections.Co } - Entry nextSegmentEntry() { while (segmentIndex >= 0) { if (segmentPositions.isEmpty()) { diff --git a/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java b/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java index 6d2c7ceef..5158a878d 100644 --- a/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java +++ b/src/test/java/net/openhft/chronicle/map/ChronicleMap8Test.java @@ -25,6 +25,7 @@ package net.openhft.chronicle.map;/* /*import junit.framework.Test; import junit.framework.TestSuite;*/ +import net.openhft.collections.KeySetView; import net.openhft.collections.SharedHashMapBuilder; import net.openhft.collections.jrs166.JSR166TestCase; import org.junit.Test; @@ -111,7 +112,7 @@ public class ChronicleMap8Test extends JSR166TestCase { * Returns a new map from Integers 1-5 to Strings "A"-"E". */ private static ConcurrentHashMap map5ConcurrentHashMap() { - ConcurrentHashMap map = new ConcurrentHashMap(5); + ConcurrentHashMap map = new ConcurrentHashMap(5); assertTrue(map.isEmpty()); map.put(one, "A"); map.put(two, "B"); @@ -305,15 +306,15 @@ public class ChronicleMap8Test extends JSR166TestCase { */ @Test public void testKeySetAddRemove() { - ChronicleMap map = map5(); - Set set1 = map.keySet(); - Set set2 = map.keySet(true); + ChronicleMap map = map5(); + final KeySetView set1 = map.keySet(); + final KeySetView set2 = map.keySet("true"); set2.add(six); - assertTrue(((ConcurrentHashMap.KeySetView) set2).getMap() == map); - assertTrue(((ConcurrentHashMap.KeySetView) set1).getMap() == map); + assertTrue(set2.getMap() == map); + assertTrue(set1.getMap() == map); assertEquals(set2.size(), map.size()); assertEquals(set1.size(), map.size()); - assertTrue((Boolean) map.get(six)); + assertTrue((Boolean.parseBoolean(map.get(six)))); assertTrue(set1.contains(six)); assertTrue(set2.contains(six)); set2.remove(six); @@ -398,8 +399,6 @@ public class ChronicleMap8Test extends JSR166TestCase { } - - /** * KeySetView.getMappedValue returns the map's mapped value */ @@ -413,38 +412,39 @@ public class ChronicleMap8Test extends JSR166TestCase { } catch (NullPointerException e) { } - final net.openhft.collections.KeySetView set = map.keySet("1"); + final net.openhft.collections.KeySetView set = map.keySet("A"); set.add(one); set.add(six); set.add(seven); - assertTrue(set.getMappedValue() == "1"); - assertTrue(map.get(one) != "1"); - assertTrue(map.get(six) == "1"); - assertTrue(map.get(seven) == "1"); + assertTrue(set.getMappedValue().equals("A")); + assertTrue(map.get(one).equals("A")); + assertTrue(map.get(six).equals("A")); + assertTrue(map.get(seven).equals("A")); } - @Test - public void testConcurrentHashMapGetMappedValue() { - ConcurrentHashMap map = map5ConcurrentHashMap(); - assertNull(map.keySet().getMappedValue()); - try { - map.keySet(null); - shouldThrow(); - } catch (NullPointerException e) { - } + /* @Test + public void testConcurrentHashMapGetMappedValue() { + ConcurrentHashMap map = map5ConcurrentHashMap(); + assertNull(map.keySet().getMappedValue()); + try { + map.keySet(null); + shouldThrow(); + } catch (NullPointerException e) { + } - final ConcurrentHashMap.KeySetView set = map.keySet("1"); + final ConcurrentHashMap.KeySetView set = map.keySet("A"); - set.add(one); - set.add(six); - set.add(seven); - assertTrue(set.getMappedValue() == "1"); - assertTrue(map.get(one) != "1"); - assertTrue(map.get(six) == "1"); - assertTrue(map.get(seven) == "1"); - } + set.add(one); + set.add(six); + set.add(seven); + assertTrue(set.getMappedValue() == "A"); + assertTrue(map.get(one) == "A"); + assertTrue(map.get(six) == "A"); + assertTrue(map.get(seven) == "A"); + } + */ void checkSpliteratorCharacteristics(Spliterator sp, int requiredCharacteristics) { assertEquals(requiredCharacteristics,