forked from EsotericSoftware/kryo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EsotericSoftware#231 from magro/master
Change KryoPool to interface + Builder, make SoftReferences optional.
- Loading branch information
Showing
6 changed files
with
309 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.esotericsoftware.kryo.pool; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.util.Queue; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
|
||
/** | ||
* A simple {@link Queue} based {@link KryoPool} implementation, should be built | ||
* using the KryoPool.Builder. | ||
* | ||
* @author Martin Grotzke | ||
*/ | ||
class KryoPoolQueueImpl implements KryoPool { | ||
|
||
private final Queue<Kryo> queue; | ||
private final KryoFactory factory; | ||
|
||
KryoPoolQueueImpl(KryoFactory factory, Queue<Kryo> queue) { | ||
this.factory = factory; | ||
this.queue = queue; | ||
} | ||
|
||
public int size () { | ||
return queue.size(); | ||
} | ||
|
||
public Kryo borrow () { | ||
Kryo res; | ||
if((res = queue.poll()) != null) { | ||
return res; | ||
} | ||
return factory.create(); | ||
} | ||
|
||
public void release (Kryo kryo) { | ||
queue.offer(kryo); | ||
} | ||
|
||
public <T> T run(KryoCallback<T> callback) { | ||
Kryo kryo = borrow(); | ||
try { | ||
return callback.execute(kryo); | ||
} finally { | ||
release(kryo); | ||
} | ||
} | ||
|
||
public void clear() { | ||
queue.clear(); | ||
} | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
src/com/esotericsoftware/kryo/pool/SoftReferenceQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.esotericsoftware.kryo.pool; | ||
|
||
import java.lang.ref.SoftReference; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Queue; | ||
|
||
import com.esotericsoftware.kryo.Kryo; | ||
|
||
/** | ||
* Internally uses {@link SoftReference}s for queued Kryo instances, | ||
* most importantly adjusts the {@link Queue#poll() poll} | ||
* behavior so that gc'ed Kryo instances are skipped. | ||
* Most other methods are unsupported. | ||
* | ||
* @author Martin Grotzke | ||
*/ | ||
class SoftReferenceQueue implements Queue<Kryo> { | ||
|
||
private Queue<SoftReference<Kryo>> delegate; | ||
|
||
public SoftReferenceQueue (Queue<?> delegate) { | ||
this.delegate = (Queue<SoftReference<Kryo>>)delegate; | ||
} | ||
|
||
public Kryo poll () { | ||
Kryo res; | ||
SoftReference<Kryo> ref; | ||
while((ref = delegate.poll()) != null) { | ||
if((res = ref.get()) != null) { | ||
return res; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean offer (Kryo e) { | ||
return delegate.offer(new SoftReference(e)); | ||
} | ||
|
||
public boolean add (Kryo e) { | ||
return delegate.add(new SoftReference(e)); | ||
} | ||
|
||
public int size () { | ||
return delegate.size(); | ||
} | ||
|
||
public boolean isEmpty () { | ||
return delegate.isEmpty(); | ||
} | ||
|
||
public boolean contains (Object o) { | ||
return delegate.contains(o); | ||
} | ||
|
||
public void clear () { | ||
delegate.clear(); | ||
} | ||
|
||
public boolean equals (Object o) { | ||
return delegate.equals(o); | ||
} | ||
|
||
public int hashCode () { | ||
return delegate.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString () { | ||
return getClass().getSimpleName() + super.toString(); | ||
} | ||
|
||
public Iterator<Kryo> iterator () { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Kryo remove () { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Object[] toArray () { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Kryo element () { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public Kryo peek () { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public <T> T[] toArray (T[] a) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public boolean remove (Object o) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public boolean containsAll (Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public boolean addAll (Collection<? extends Kryo> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public boolean removeAll (Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public boolean retainAll (Collection<?> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
Oops, something went wrong.