Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Apr 17, 2012
1 parent 351366d commit 80859a2
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 45 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo</artifactId>
<version>2.01</version>
<version>2.05</version>
<packaging>jar</packaging>
<name>Kryo</name>
<description>Fast, efficient Java serialization</description>
Expand Down Expand Up @@ -47,6 +47,11 @@
<artifactId>minlog</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
18 changes: 11 additions & 7 deletions src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Currency;
Expand All @@ -24,7 +23,6 @@
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.ArraySerializer;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.ArraysAsListSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.BigDecimalSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.BigIntegerSerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.BooleanSerializer;
Expand Down Expand Up @@ -101,7 +99,6 @@ public Kryo () {
addDefaultSerializer(Currency.class, CurrencySerializer.class);
addDefaultSerializer(StringBuffer.class, StringBufferSerializer.class);
addDefaultSerializer(StringBuilder.class, StringBuilderSerializer.class);
addDefaultSerializer(Arrays.asList().getClass(), ArraysAsListSerializer.class);
addDefaultSerializer(Collections.EMPTY_LIST.getClass(), CollectionsEmptyListSerializer.class);
addDefaultSerializer(Collections.EMPTY_MAP.getClass(), CollectionsEmptyMapSerializer.class);
addDefaultSerializer(Collections.EMPTY_SET.getClass(), CollectionsEmptySetSerializer.class);
Expand Down Expand Up @@ -180,14 +177,21 @@ public void addDefaultSerializer (Class type, Serializer serializer) {
* <td>Date</td>
* <td>Enum</td>
* <td>Currency</td>
* </tr>
* <tr>
* <td>Map</td>
* <td>Collection</td>
* <td>Collections.emptyList</td>
* <td>Collections.emptyMap</td>
* <td>Collections.emptySet</td>
* <td>KryoSerializable</td>
* </tr>
* <tr>
* <td>StringBuffer</td>
* <td>StringBuilder</td>
* <td>Collection</td>
* <td>Map</td>
* <td>Serializable</td>
* <td>Arrays.asList</td>
* <td>Collections.singletonList</td>
* <td>Collections.singletonMap</td>
* <td>Collections.singleton</td>
* </tr>
* </table>
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public int read () throws IOException {
public int read (byte[] bytes, int offset, int length) throws IOException {
int count = Math.min(byteBuffer.remaining(), length);
if (count == 0) return -1;
byteBuffer.get(bytes, offset, length);
byteBuffer.get(bytes, offset, count);
return count;
}

Expand Down
14 changes: 0 additions & 14 deletions src/com/esotericsoftware/kryo/serializers/DefaultSerializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Currency;
import java.util.Date;
Expand Down Expand Up @@ -298,17 +295,6 @@ public void read (Kryo kryo, Input input, KryoSerializable object) {
}
}

/** Serializer for lists created via {@link Arrays#asList(Object...)}. */
static public class ArraysAsListSerializer extends CollectionSerializer {
public ArraysAsListSerializer (Kryo kryo) {
super(kryo);
}

public Collection create (Kryo kryo, Input input, Class type) {
return new ArrayList();
}
}

/** Serializer for lists created via {@link Collections#emptyList()} or that were just assigned the
* {@link Collections#EMPTY_LIST}.
* @author <a href="mailto:[email protected]">Martin Grotzke</a> */
Expand Down
203 changes: 187 additions & 16 deletions src/com/esotericsoftware/kryo/util/ObjectMap.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* 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.esotericsoftware.kryo.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;

/** An unordered map. This implementation is a cuckoo hash map using 3 hashes, random walking, and a small stash for problematic
Expand All @@ -29,6 +17,7 @@ public class ObjectMap<K, V> {
static private final int PRIME1 = 0xbe1f14b1;
static private final int PRIME2 = 0xb4b82e39;
static private final int PRIME3 = 0xced1c241;

static Random random = new Random();

public int size;
Expand All @@ -42,6 +31,10 @@ public class ObjectMap<K, V> {
private int stashCapacity;
private int pushIterations;

private Entries entries;
private Values values;
private Keys keys;

/** Creates a new map with an initial capacity of 32 and a load factor of 0.8. This map will hold 25 items before growing the
* backing table. */
public ObjectMap () {
Expand Down Expand Up @@ -133,6 +126,11 @@ private V put_internal (K key, V value) {
return null;
}

public void putAll (ObjectMap<K, V> map) {
for (Entry<K, V> entry : map.entries())
put(entry.key, entry.value);
}

/** Skips checks for existing keys. */
private void putResize (K key, V value) {
// Check for empty buckets.
Expand Down Expand Up @@ -476,7 +474,180 @@ public String toString () {
return buffer.toString();
}

static int nextPowerOfTwo (int value) {
/** Returns an iterator for the entries in the map. Remove is supported. Note that the same iterator instance is returned each
* time this method is called. Use the {@link Entries} constructor for nested or multithreaded iteration. */
public Entries<K, V> entries () {
if (entries == null)
entries = new Entries(this);
else
entries.reset();
return entries;
}

/** Returns an iterator for the values in the map. Remove is supported. Note that the same iterator instance is returned each
* time this method is called. Use the {@link Entries} constructor for nested or multithreaded iteration. */
public Values<V> values () {
if (values == null)
values = new Values(this);
else
values.reset();
return values;
}

/** Returns an iterator for the keys in the map. Remove is supported. Note that the same iterator instance is returned each time
* this method is called. Use the {@link Entries} constructor for nested or multithreaded iteration. */
public Keys<K> keys () {
if (keys == null)
keys = new Keys(this);
else
keys.reset();
return keys;
}

static public class Entry<K, V> {
public K key;
public V value;

public String toString () {
return key + "=" + value;
}
}

static private class MapIterator<K, V> {
public boolean hasNext;

protected final ObjectMap<K, V> map;
int currentIndex;
protected int nextIndex;

public MapIterator (ObjectMap<K, V> map) {
this.map = map;
reset();
}

public void reset () {
currentIndex = -1;
nextIndex = -1;
advance();
}

protected void advance () {
hasNext = false;
K[] keyTable = map.keyTable;
for (int n = map.capacity + map.stashSize; ++nextIndex < n;) {
if (keyTable[nextIndex] != null) {
hasNext = true;
break;
}
}
}

public void remove () {
if (currentIndex < 0) throw new IllegalStateException("next must be called before remove.");
if (currentIndex >= map.capacity) {
map.removeStashIndex(currentIndex);
} else {
map.keyTable[currentIndex] = null;
map.valueTable[currentIndex] = null;
}
currentIndex = -1;
map.size--;
}
}

static public class Entries<K, V> extends MapIterator<K, V> implements Iterable<Entry<K, V>>, Iterator<Entry<K, V>> {
protected Entry<K, V> entry = new Entry();

public Entries (ObjectMap<K, V> map) {
super(map);
}

/** Note the same entry instance is returned each time this method is called. */
public Entry<K, V> next () {
if (!hasNext) throw new NoSuchElementException();
K[] keyTable = map.keyTable;
entry.key = keyTable[nextIndex];
entry.value = map.valueTable[nextIndex];
currentIndex = nextIndex;
advance();
return entry;
}

public boolean hasNext () {
return hasNext;
}

public Iterator<Entry<K, V>> iterator () {
return this;
}
}

static public class Values<V> extends MapIterator<Object, V> implements Iterable<V>, Iterator<V> {
public Values (ObjectMap<?, V> map) {
super((ObjectMap<Object, V>)map);
}

public boolean hasNext () {
return hasNext;
}

public V next () {
V value = map.valueTable[nextIndex];
currentIndex = nextIndex;
advance();
return value;
}

public Iterator<V> iterator () {
return this;
}

/** Returns a new array containing the remaining values. */
public ArrayList<V> toArray () {
ArrayList array = new ArrayList(map.size);
while (hasNext)
array.add(next());
return array;
}

/** Adds the value entries to the given array.
* @param array */
public void toArray (ArrayList<V> array) {
while (hasNext)
array.add(next());
}
}

static public class Keys<K> extends MapIterator<K, Object> implements Iterable<K>, Iterator<K> {
public Keys (ObjectMap<K, ?> map) {
super((ObjectMap<K, Object>)map);
}

public boolean hasNext () {
return hasNext;
}

public K next () {
K key = map.keyTable[nextIndex];
currentIndex = nextIndex;
advance();
return key;
}

public Iterator<K> iterator () {
return this;
}

/** Returns a new array containing the remaining keys. */
public ArrayList<K> toArray () {
ArrayList array = new ArrayList(map.size);
while (hasNext)
array.add(next());
return array;
}
}

static public int nextPowerOfTwo (int value) {
if (value == 0) return 1;
value--;
value |= value >> 1;
Expand Down
6 changes: 0 additions & 6 deletions test/com/esotericsoftware/kryo/DefaultSerializersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,6 @@ public void testEnumSerializerWithMethods () {
roundTrip(75, TestEnumWithMethods.c);
}

public void testArraysAsList () {
kryo.setRegistrationRequired(false);
List<Integer> test = Arrays.asList(1, 2, 3);
roundTrip(36, test);
}

public void testCollectionsMethods () {
kryo.setRegistrationRequired(false);
ArrayList test = new ArrayList();
Expand Down

0 comments on commit 80859a2

Please sign in to comment.