This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ParkJeongHwan
committed
Sep 18, 2019
1 parent
e583044
commit bfc6a81
Showing
4 changed files
with
146 additions
and
6 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
55 changes: 55 additions & 0 deletions
55
src/main/java/com/realtimetech/kson/builder/KsonBuilder.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,55 @@ | ||
package com.realtimetech.kson.builder; | ||
|
||
import java.util.HashMap; | ||
|
||
import com.realtimetech.kson.KsonContext; | ||
import com.realtimetech.kson.transform.Transformer; | ||
|
||
public class KsonBuilder { | ||
private HashMap<Class<?>, Transformer<?>> registeredTransformers; | ||
|
||
private int stackSize; | ||
private int stringBufferSize; | ||
|
||
public KsonBuilder() { | ||
this.registeredTransformers = new HashMap<Class<? extends Object>, Transformer<? extends Object>>(); | ||
this.stackSize = 10; | ||
this.stringBufferSize = 100; | ||
} | ||
|
||
public KsonBuilder registerTransformer(Class<?> clazz, Transformer<?> preTransformer) { | ||
this.registeredTransformers.put(clazz, preTransformer); | ||
|
||
return this; | ||
} | ||
|
||
public int getStackSize() { | ||
return stackSize; | ||
} | ||
|
||
public KsonBuilder setStackSize(int stackSize) { | ||
this.stackSize = stackSize; | ||
|
||
return this; | ||
} | ||
|
||
public int getStringBufferSize() { | ||
return stringBufferSize; | ||
} | ||
|
||
public KsonBuilder setStringBufferSize(int stringBufferSize) { | ||
this.stringBufferSize = stringBufferSize; | ||
|
||
return this; | ||
} | ||
|
||
public KsonContext build() { | ||
KsonContext ksonContext = new KsonContext(this.stackSize, this.stringBufferSize); | ||
|
||
for (Class<?> clazz : this.registeredTransformers.keySet()) { | ||
ksonContext.registerTransformer(clazz, this.registeredTransformers.get(clazz)); | ||
} | ||
|
||
return ksonContext; | ||
} | ||
} |
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,42 @@ | ||
package com.realtimetech.kson.pool; | ||
|
||
import java.util.HashMap; | ||
|
||
import com.realtimetech.kson.KsonContext; | ||
import com.realtimetech.kson.builder.KsonBuilder; | ||
|
||
public class KsonPool { | ||
private HashMap<Thread, KsonContext> contextPools; | ||
|
||
private KsonBuilder ksonBuilder; | ||
|
||
public KsonPool(KsonBuilder ksonBuilder) { | ||
this.contextPools = new HashMap<Thread, KsonContext>(); | ||
|
||
this.ksonBuilder = ksonBuilder; | ||
} | ||
|
||
public synchronized KsonContext get() { | ||
Thread currentThread = Thread.currentThread(); | ||
|
||
if (!this.contextPools.containsKey(currentThread)) { | ||
KsonContext freeContext = null; | ||
|
||
for (Thread thread : this.contextPools.keySet()) { | ||
if (!thread.isAlive()) { | ||
freeContext = this.contextPools.get(thread); | ||
this.contextPools.remove(thread); | ||
break; | ||
} | ||
} | ||
|
||
if (freeContext == null) { | ||
freeContext = ksonBuilder.build(); | ||
} | ||
|
||
this.contextPools.put(currentThread, freeContext); | ||
} | ||
|
||
return this.contextPools.get(currentThread); | ||
} | ||
} |
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