1
0
mirror of https://github.com/Rogiel/l2jserver2 synced 2025-12-05 23:22:47 +00:00

Implements character service configuration

This commit is contained in:
2011-12-29 02:27:42 -02:00
parent c5911ea884
commit f2cf139d34
18 changed files with 566 additions and 23 deletions

View File

@@ -50,6 +50,22 @@ public class ArrayUtils {
values.add(item);
}
}
return (T[]) Arrays.copyOf(values.toArray(), values.size(), array.getClass());
return (T[]) Arrays.copyOf(values.toArray(), values.size(),
array.getClass());
}
/**
* Searches for the <code>expected</code> item to be in the
* <code>array</code>.
*
* @param array
* the array to search in
* @param expected
* the item to be looked in the array
* @return <code>true</code> if the item exists, <code>false</code>
* otherwise
*/
public final static <T> boolean contains(T[] array, T expected) {
return Arrays.binarySearch(array, expected) >= 0;
}
}

View File

@@ -22,6 +22,7 @@ import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import com.l2jserver.util.transformer.impl.ArrayTransformer;
import com.l2jserver.util.transformer.impl.BooleanTransformer;
import com.l2jserver.util.transformer.impl.ByteTransformer;
import com.l2jserver.util.transformer.impl.ClassTransformer;
@@ -34,6 +35,7 @@ import com.l2jserver.util.transformer.impl.IntegerTransformer;
import com.l2jserver.util.transformer.impl.LongTransformer;
import com.l2jserver.util.transformer.impl.PathTransformer;
import com.l2jserver.util.transformer.impl.ShortTransformer;
import com.l2jserver.util.transformer.impl.StringTransformer;
import com.l2jserver.util.transformer.impl.URITransformer;
import com.l2jserver.util.transformer.impl.URLTransformer;
@@ -78,9 +80,12 @@ public class TransformerFactory {
return URLTransformer.SHARED_INSTANCE;
} else if (type == Path.class) {
return PathTransformer.SHARED_INSTANCE;
} else if(type.isEnum()) {
} else if (type.isEnum()) {
return EnumTransformer.SHARED_INSTANCE;
}
} else if(type.isArray()) {
return ArrayTransformer.SHARED_INSTANCE;
} else if (type == String.class)
return StringTransformer.SHARED_INSTANCE;
return null;
}
}

View File

@@ -0,0 +1,72 @@
/*
* This file is part of l2jserver2 <l2jserver2.com>.
*
* l2jserver2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver2. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.util.transformer.impl;
import java.lang.reflect.Array;
import org.apache.commons.lang3.StringUtils;
import com.l2jserver.util.transformer.Transformer;
import com.l2jserver.util.transformer.TransformerFactory;
/**
* Transform an {@link Array} into an string.
* <p>
* <b>Important note</b>: Array elements are by an <code>|</code>.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
* @param <T>
* the array component type that this tranformer transforms
*/
public class ArrayTransformer<T> implements Transformer<T[]> {
/**
* This transformer shared instance
*/
@SuppressWarnings("rawtypes")
public static final ArrayTransformer<?> SHARED_INSTANCE = new ArrayTransformer();
@Override
@SuppressWarnings("unchecked")
public String transform(Class<? extends T[]> type, T[] value) {
final Transformer<T> transformer = (Transformer<T>) TransformerFactory
.getTransfromer(type.getComponentType());
final String[] values = new String[value.length];
int i = 0;
for (final T item : value) {
values[i++] = transformer.transform(
(Class<? extends T>) type.getComponentType(), item);
}
return StringUtils.join(values, '|');
}
@Override
@SuppressWarnings("unchecked")
public T[] untransform(Class<? extends T[]> type, String stringValue) {
final Transformer<T> transformer = (Transformer<T>) TransformerFactory
.getTransfromer(type.getComponentType());
final String[] stringValues = StringUtils.split(stringValue, '|');
final T[] values = (T[]) Array.newInstance(type.getComponentType(),
stringValues.length);
int i = 0;
for (final String value : stringValues) {
values[i++] = transformer.untransform(
(Class<? extends T>) type.getComponentType(), value);
}
return values;
}
}

View File

@@ -0,0 +1,42 @@
/*
* This file is part of l2jserver2 <l2jserver2.com>.
*
* l2jserver2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver2. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.util.transformer.impl;
import com.l2jserver.util.transformer.Transformer;
/**
* This tranformer does nothing!
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public class StringTransformer implements Transformer<String> {
/**
* This transformer shared instance
*/
public static final StringTransformer SHARED_INSTANCE = new StringTransformer();
@Override
public String transform(Class<? extends String> type, String value) {
return value;
}
@Override
public String untransform(Class<? extends String> type, String value) {
return value;
}
}

View File

@@ -0,0 +1,94 @@
/*
* This file is part of l2jserver2 <l2jserver2.com>.
*
* l2jserver2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* l2jserver2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with l2jserver2. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jserver.util.transformer.impl;
import org.junit.Assert;
import org.junit.Test;
/**
* @author <a href="http://www.rogiel.com">Rogiel</a>
*
*/
public class ArrayTransformerTest {
/**
* An Integer[] array as an string
*/
private static final String INT_ARRAY_STRING = "1|2|3";
/**
* An Integer[] array
*/
private static final Integer[] INT_ARRAY = new Integer[] { 1, 2, 3 };
/**
* An String[] array as an string
*/
private static final String STRING_ARRAY_STRING = "test1|test2|test3";
/**
* An String[] array
*/
private static final String[] STRING_ARRAY = new String[] { "test1",
"test2", "test3" };
/**
* An Class[] array as an string
*/
private static final String CLASS_ARRAY_STRING = "java.lang.Object|java.lang.Integer|java.lang.Long";
/**
* An Class[] array
*/
private static final Class<?>[] CLASS_ARRAY = new Class<?>[] {
Object.class, Integer.class, Long.class };
/**
* Tests transforming an {@link Integer} array
*/
@Test
public void testIntegerTransforming() {
final ArrayTransformer<Integer> transformer = new ArrayTransformer<Integer>();
Assert.assertEquals(INT_ARRAY_STRING,
transformer.transform(Integer[].class, INT_ARRAY));
Assert.assertArrayEquals(INT_ARRAY,
transformer.untransform(Integer[].class, INT_ARRAY_STRING));
}
/**
* Tests transforming an {@link String} array
*/
@Test
public void testStringTransforming() {
final ArrayTransformer<String> transformer = new ArrayTransformer<String>();
Assert.assertEquals(STRING_ARRAY_STRING,
transformer.transform(String[].class, STRING_ARRAY));
Assert.assertArrayEquals(STRING_ARRAY,
transformer.untransform(String[].class, STRING_ARRAY_STRING));
}
/**
* Tests transforming an {@link Class} array
*/
@Test
@SuppressWarnings("unchecked")
public void testClassTransforming() {
final ArrayTransformer<Class<?>> transformer = new ArrayTransformer<Class<?>>();
Assert.assertEquals(CLASS_ARRAY_STRING, transformer.transform(
(Class<? extends Class<?>[]>) Class[].class, CLASS_ARRAY));
Assert.assertArrayEquals(CLASS_ARRAY, transformer
.untransform((Class<? extends Class<?>[]>) Class[].class,
CLASS_ARRAY_STRING));
}
}