1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-08 08:23:11 +00:00

Removed "core" project dependency

Signed-off-by: Rogiel <rogiel@rogiel.com>
This commit is contained in:
2011-05-13 17:12:29 -03:00
parent bb3f24e8f4
commit 73d793c28c
47 changed files with 1402 additions and 39 deletions

View File

@@ -0,0 +1,31 @@
package com.l2jserver.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ArrayIterator<T> implements Iterator<T> {
private final T[] objects;
private int i = 0;
public ArrayIterator(T... objects) {
this.objects = objects;
}
@Override
public boolean hasNext() {
return i < objects.length;
}
@Override
public T next() {
try {
return objects[i++];
} catch (ArrayIndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
@Override
public void remove() {
}
}

View File

@@ -0,0 +1,92 @@
package com.l2jserver.util.factory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* Factory class to create {@link Collection} instances.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class CollectionFactory {
/**
* Creates a new list of type <tt>T</tt>
*
* @param <T>
* the type
* @param type
* the type
* @return the created list
*/
public static final <T> List<T> newList(Class<T> type) {
return new ArrayList<T>();
}
/**
* Creates a new set of type <tt>T</tt>
*
* @param <T>
* the type
* @param type
* the type
* @return the created set
*/
public static final <T> Set<T> newSet(Class<T> type) {
return new HashSet<T>();
}
/**
* Creates a new concurrent queue of type <tt>T</tt>
*
* @param <T>
* the type
* @param type
* the type
* @return the created queue
*/
public static final <T> Queue<T> newConcurrentQueue(Class<T> type) {
return new ConcurrentLinkedQueue<T>();
}
/**
* Creates a new map.
*
* @param <K>
* the key type
* @param <V>
* the value type
* @param key
* the key type class
* @param value
* the value type class
* @return the new map
*/
public static final <K, V> Map<K, V> newMap(Class<K> key, Class<V> value) {
return new HashMap<K, V>();
}
/**
* Creates a new weak map.
*
* @param <K>
* the key type
* @param <V>
* the value type
* @param key
* the key type class
* @param value
* the value type class
* @return the new map
*/
public static final <K, V> Map<K, V> newWeakMap(Class<K> key, Class<V> value) {
return new WeakHashMap<K, V>();
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.util.transformer;
/**
* An transformer can transform an {@link Object} into an {@link String} and the
* {@link String} back to an equivalent object.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public interface Transformer<T> {
/**
* Transform the object in a string
*
* @param value
* the object
* @return the string of the object
*/
String transform(T value);
/**
* Untransforms the string back to an object
*
* @param value
* the string
* @return the object
*/
T untransform(String value);
}

View File

@@ -0,0 +1,42 @@
package com.l2jserver.util.transformer;
import java.io.File;
import java.net.InetSocketAddress;
import com.l2jserver.util.transformer.impl.BooleanTransformer;
import com.l2jserver.util.transformer.impl.ByteTransformer;
import com.l2jserver.util.transformer.impl.ClassTransformer;
import com.l2jserver.util.transformer.impl.DoubleTransformer;
import com.l2jserver.util.transformer.impl.FileTransformer;
import com.l2jserver.util.transformer.impl.FloatTransformer;
import com.l2jserver.util.transformer.impl.InetSocketAddressTransformer;
import com.l2jserver.util.transformer.impl.IntegerTransformer;
import com.l2jserver.util.transformer.impl.LongTransformer;
import com.l2jserver.util.transformer.impl.ShortTransformer;
public class TransformerFactory {
public static final Transformer<?> getTransfromer(Class<?> type) {
if (type == Byte.class || type == Byte.TYPE) {
return ByteTransformer.SHARED_INSTANCE;
} else if (type == Short.class || type == Short.TYPE) {
return ShortTransformer.SHARED_INSTANCE;
} else if (type == Integer.class || type == Integer.TYPE) {
return IntegerTransformer.SHARED_INSTANCE;
} else if (type == Long.class || type == Long.TYPE) {
return LongTransformer.SHARED_INSTANCE;
} else if (type == Float.class || type == Float.TYPE) {
return FloatTransformer.SHARED_INSTANCE;
} else if (type == Double.class || type == Double.TYPE) {
return DoubleTransformer.SHARED_INSTANCE;
} else if (type == Boolean.class || type == Boolean.TYPE) {
return BooleanTransformer.SHARED_INSTANCE;
} else if(type == InetSocketAddress.class) {
return InetSocketAddressTransformer.SHARED_INSTANCE;
} else if(type == File.class) {
return FileTransformer.SHARED_INSTANCE;
}else if(type == Class.class) {
return ClassTransformer.SHARED_INSTANCE;
}
return null;
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Boolean} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class BooleanTransformer implements Transformer<Boolean> {
public static final BooleanTransformer SHARED_INSTANCE = new BooleanTransformer();
@Override
public String transform(Boolean value) {
return (value ? "true" : "false");
}
@Override
public Boolean untransform(String value) {
return Boolean.parseBoolean(value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ByteTransformer implements Transformer<Byte> {
public static final ByteTransformer SHARED_INSTANCE = new ByteTransformer();
@Override
public String transform(Byte value) {
return Double.toString(value);
}
@Override
public Byte untransform(String value) {
return Byte.decode(value);
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Class} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ClassTransformer implements Transformer<Class<?>> {
public static final ClassTransformer SHARED_INSTANCE = new ClassTransformer();
@Override
public String transform(Class<?> value) {
return value.getName();
}
@Override
public Class<?> untransform(String value) {
try {
return Class.forName(value);
} catch (ClassNotFoundException e) {
return null;
}
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class DoubleTransformer implements Transformer<Double> {
public static final DoubleTransformer SHARED_INSTANCE = new DoubleTransformer();
@Override
public String transform(Double value) {
return Double.toString(value);
}
@Override
public Double untransform(String value) {
return Double.parseDouble(value);
}
}

View File

@@ -0,0 +1,27 @@
package com.l2jserver.util.transformer.impl;
import java.io.File;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class FileTransformer implements Transformer<File> {
public static final FileTransformer SHARED_INSTANCE = new FileTransformer();
private final File root = new File("./");
@Override
public String transform(File value) {
return value.getAbsolutePath();
}
@Override
public File untransform(String value) {
return new File(root, value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class FloatTransformer implements Transformer<Float> {
public static final FloatTransformer SHARED_INSTANCE = new FloatTransformer();
@Override
public String transform(Float value) {
return Double.toString(value);
}
@Override
public Float untransform(String value) {
return Float.parseFloat(value);
}
}

View File

@@ -0,0 +1,29 @@
package com.l2jserver.util.transformer.impl;
import java.net.InetSocketAddress;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class InetSocketAddressTransformer implements
Transformer<InetSocketAddress> {
public static final InetSocketAddressTransformer SHARED_INSTANCE = new InetSocketAddressTransformer();
@Override
public String transform(InetSocketAddress value) {
return value.getHostName() + ":" + value.getPort();
}
@Override
public InetSocketAddress untransform(String value) {
final String[] pieces = value.split(":");
if (pieces.length != 2)
return null;
return new InetSocketAddress(pieces[0], Integer.parseInt(pieces[1]));
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class IntegerTransformer implements Transformer<Integer> {
public static final IntegerTransformer SHARED_INSTANCE = new IntegerTransformer();
@Override
public String transform(Integer value) {
return Integer.toString(value);
}
@Override
public Integer untransform(String value) {
return Integer.decode(value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Long} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class LongTransformer implements Transformer<Long> {
public static final LongTransformer SHARED_INSTANCE = new LongTransformer();
@Override
public String transform(Long value) {
return Long.toString(value);
}
@Override
public Long untransform(String value) {
return Long.decode(value);
}
}

View File

@@ -0,0 +1,23 @@
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* Transform an {@link Integer} into an string.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class ShortTransformer implements Transformer<Short> {
public static final ShortTransformer SHARED_INSTANCE = new ShortTransformer();
@Override
public String transform(Short value) {
return Short.toString(value);
}
@Override
public Short untransform(String value) {
return Short.decode(value);
}
}