Changed folder structure of test classes and renamed some packages
This commit is contained in:
parent
ccd50ec104
commit
884c5d64c3
76 changed files with 5305 additions and 5375 deletions
460
src/zutil/converter/Converter.java
Executable file
460
src/zutil/converter/Converter.java
Executable file
|
|
@ -0,0 +1,460 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.converter;
|
||||
|
||||
import zutil.io.DynamicByteArrayStream;
|
||||
import zutil.parser.Base64Decoder;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.BitSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class Converter {
|
||||
private Converter(){}
|
||||
|
||||
/**
|
||||
* Converts an object to an array of bytes.
|
||||
*
|
||||
* @param object the object to convert.
|
||||
* @return the associated byte array.
|
||||
*/
|
||||
public static byte[] toBytes(Object object) throws IOException{
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(object);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Integer to an byte array
|
||||
*
|
||||
* @param num is the number to convert
|
||||
* @return a byte array of four bytes
|
||||
*/
|
||||
public static byte[] toBytes(int num){
|
||||
return new byte[]{
|
||||
(byte)(num & 0xff),
|
||||
(byte)((num >> 8)& 0xff),
|
||||
(byte)((num >> 16)& 0xff),
|
||||
(byte)((num >> 24)& 0xff)};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Integer to a byte
|
||||
*
|
||||
* @param num is the number to convert
|
||||
* @return a byte value of the integer
|
||||
*/
|
||||
public static byte toByte(int num){
|
||||
return (byte)(num & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts hex string to a byte array
|
||||
*
|
||||
* @param hex a String containing data coded in hex
|
||||
* @return a byte array
|
||||
*/
|
||||
public static byte[] hexToByte(String hex){
|
||||
if(hex == null)
|
||||
return null;
|
||||
if(hex.startsWith("0x"))
|
||||
hex = hex.substring(2);
|
||||
byte[] b = new byte[(int)Math.ceil(hex.length()/2.0)];
|
||||
for(int i=hex.length()-1; i>=0; i-=2){
|
||||
if(i-1 < 0)
|
||||
b[(hex.length()-i-1)/2] = hexToByte(hex.charAt(i));
|
||||
else
|
||||
b[(hex.length()-i-1)/2] = hexToByte(hex.charAt(i-1), hex.charAt(i));
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts hex chars to a byte
|
||||
*
|
||||
* @param quad1 is the first hex value
|
||||
* @param quad2 is the second hex value
|
||||
* @return a byte that corresponds to the hex
|
||||
*/
|
||||
public static byte hexToByte( char quad1, char quad2){
|
||||
byte b = hexToByte( quad2 );
|
||||
b |= hexToByte( quad1 ) << 4;
|
||||
return b;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a hex chars to a byte
|
||||
*
|
||||
* @param hex is the hex value
|
||||
* @return a byte that corresponds to the hex
|
||||
*/
|
||||
public static byte hexToByte( char hex ){
|
||||
switch( Character.toLowerCase(hex) ){
|
||||
case '0': return 0x00;
|
||||
case '1': return 0x01;
|
||||
case '2': return 0x02;
|
||||
case '3': return 0x03;
|
||||
case '4': return 0x04;
|
||||
case '5': return 0x05;
|
||||
case '6': return 0x06;
|
||||
case '7': return 0x07;
|
||||
case '8': return 0x08;
|
||||
case '9': return 0x09;
|
||||
case 'a': return 0x0a;
|
||||
case 'b': return 0x0b;
|
||||
case 'c': return 0x0c;
|
||||
case 'd': return 0x0d;
|
||||
case 'e': return 0x0e;
|
||||
case 'f': return 0x0f;
|
||||
}
|
||||
return (byte)0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of bytes back to its constituent object. The
|
||||
* input array is assumed to have been created from the original object.
|
||||
*
|
||||
* @param bytes the byte array to convert.
|
||||
* @return the associated object.
|
||||
*/
|
||||
public static Object toObject(byte[] bytes) throws Exception{
|
||||
Object object = null;
|
||||
|
||||
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
|
||||
object = ois.readObject();
|
||||
ois.close();
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of bytes back to its constituent object. The
|
||||
* input array is assumed to have been created from the original object.
|
||||
*
|
||||
* @param bytes the byte array to convert.
|
||||
* @return the associated object.
|
||||
*/
|
||||
public static Object toObject(DynamicByteArrayStream bytes) throws Exception{
|
||||
Object object = null;
|
||||
|
||||
ObjectInputStream ois = new ObjectInputStream(bytes);
|
||||
object = ois.readObject();
|
||||
ois.close();
|
||||
|
||||
return object;
|
||||
}
|
||||
|
||||
|
||||
/** array needed for byteToHex */
|
||||
private static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
|
||||
/**
|
||||
* Converts a byte Array to a Hex String
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a hex String
|
||||
*/
|
||||
public static String toHexString(byte[][] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
||||
for(byte[] a : raw){
|
||||
for(byte b : a){
|
||||
ret.append(HEX_CHARS[(int) (b >>> 0x04)& 0x0F ]);
|
||||
ret.append(HEX_CHARS[(int) b & 0x0F ]);
|
||||
}
|
||||
}
|
||||
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
public static String toHexStringByColumn(byte[][] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
||||
for(int col=0; col<raw[0].length ;col++){
|
||||
for(int row=0; row<raw.length ;row++){
|
||||
ret.append(HEX_CHARS[(int) (raw[row][col] >>> 0x04)& 0x0F ]);
|
||||
ret.append(HEX_CHARS[(int) raw[row][col] & 0x0F ]);
|
||||
}
|
||||
}
|
||||
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a byte Array to a Hex String
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a hex String
|
||||
*/
|
||||
public static String toHexString(byte[] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
|
||||
for(byte b : raw){
|
||||
ret.append(HEX_CHARS[(int) (b >>> 0x04)& 0x0F ]);
|
||||
ret.append(HEX_CHARS[(int) b & 0x0F ]);
|
||||
}
|
||||
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a byte to a Hex String
|
||||
*
|
||||
* @param raw the byte to convert
|
||||
* @return a hex String
|
||||
*/
|
||||
public static String toHexString(byte raw){
|
||||
String ret = ""+HEX_CHARS[(int) (raw >>> 0x04)& 0x0F ];
|
||||
ret += ""+HEX_CHARS[(int) raw & 0x0F ];
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given byte to a String with 1's and 0's
|
||||
*
|
||||
* @param raw the byte to convert
|
||||
* @return a String with 1's and 0's
|
||||
*/
|
||||
public static String toString(byte raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
for(int i=128; i>0 ;i=( i<1 ? i=0 : i/2 ) ){
|
||||
ret.append(( (raw & i) == 0 ? '0' : '1'));
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given byte array to a String with 1's and 0's
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a String with 1's and 0's
|
||||
*/
|
||||
public static String toString(byte[] raw){
|
||||
StringBuffer ret = new StringBuffer();
|
||||
for(byte b : raw){
|
||||
for(int i=128; i>0 ;i=( i<1 ? i=0 : i/2 ) ){
|
||||
ret.append(( (b & i) == 0 ? '0' : '1'));
|
||||
}
|
||||
}
|
||||
return ret.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a comma separated string with key and value pairs
|
||||
*
|
||||
* @return a comma separated String
|
||||
*/
|
||||
public static String toString(Map map){
|
||||
StringBuilder tmp = new StringBuilder();
|
||||
tmp.append("{");
|
||||
Iterator<Object> it = map.keySet().iterator();
|
||||
while(it.hasNext()){
|
||||
Object key = it.next();
|
||||
Object value = map.get(key);
|
||||
tmp.append(key);
|
||||
if (value != null) {
|
||||
if (value instanceof String)
|
||||
tmp.append(": \"").append(value).append("\"");
|
||||
else
|
||||
tmp.append(value);
|
||||
}
|
||||
else
|
||||
tmp.append("null");
|
||||
|
||||
if(it.hasNext())
|
||||
tmp.append(", ");
|
||||
}
|
||||
tmp.append('}');
|
||||
return tmp.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a BitSet to a Integer
|
||||
*
|
||||
* @param bits the BitSet to convert
|
||||
* @return a Integer
|
||||
*/
|
||||
public static int toInt(BitSet bits){
|
||||
int ret = 0;
|
||||
|
||||
for (int i = bits.nextSetBit(0); i >= 0; i = bits.nextSetBit(i+1)) {
|
||||
ret += Math.pow(2, i);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a boolean array(bit sequence whit most significant bit at index 0) to a Integer
|
||||
*
|
||||
* @param bits the boolean array to convert
|
||||
* @return a Integer
|
||||
*/
|
||||
public static int toInt(boolean[] bits){
|
||||
int ret = 0;
|
||||
|
||||
for (int i = bits.length-1; i >= 0; i--) {
|
||||
if(bits[i])ret += Math.pow(2, bits.length-i-1);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a byte to a integer
|
||||
*
|
||||
* @param b is the byte to convert
|
||||
* @return the integer value of the byte
|
||||
*/
|
||||
public static int toInt(byte b){
|
||||
return (int)(b & 0xff);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a dynamic sized byte array to a integer
|
||||
*
|
||||
* @param b is the byte array of size 1-4
|
||||
* @return the int value of the byte array
|
||||
*/
|
||||
public static int toInt(byte[] b){
|
||||
int i = 0;
|
||||
switch (b.length){
|
||||
default:
|
||||
case 4:
|
||||
i |= 0xFF000000 & (b[0] << 24);
|
||||
i |= 0x00FF0000 & (b[1] << 16);
|
||||
i |= 0x0000FF00 & (b[2] << 8);
|
||||
i |= 0x000000FF & b[3];
|
||||
break;
|
||||
case 3:
|
||||
i |= 0x00FF0000 & (b[0] << 16);
|
||||
i |= 0x0000FF00 & (b[1] << 8);
|
||||
i |= 0x000000FF & b[2];
|
||||
break;
|
||||
case 2:
|
||||
i |= 0x0000FF00 & (b[0] << 8);
|
||||
i |= 0x000000FF & b[1];
|
||||
break;
|
||||
case 1:
|
||||
i |= 0x000000FF & b[0];
|
||||
break;
|
||||
case 0: break;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a Integer to a BitSet
|
||||
*
|
||||
* @param num the Integer to convert
|
||||
* @return a BitSet object
|
||||
*/
|
||||
public static BitSet toBitSet(int num){
|
||||
BitSet ret = new BitSet();
|
||||
String tmp = Integer.toBinaryString(num);
|
||||
|
||||
for(int i=0; i<tmp.length() ;i++){
|
||||
ret.set(i , tmp.charAt(tmp.length()-i-1) != '0');
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a given String to a specified class
|
||||
*
|
||||
* @param <T> is the resulting class
|
||||
* @param data is the String data to be converted
|
||||
* @param c is the class to convert to
|
||||
* @return a instance of the class with the value in the string or null if there was an problem
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <T> T fromString(String data, Class<T> c){
|
||||
if(data == null || data.isEmpty())
|
||||
return null;
|
||||
try{
|
||||
if( c == String.class) return (T) data;
|
||||
else if(c == Integer.class) return (T) new Integer(data);
|
||||
else if(c == int.class) return (T) new Integer(data);
|
||||
else if(c == Long.class) return (T) new Long(data);
|
||||
else if(c == long.class) return (T) new Long(data);
|
||||
else if(c == Float.class) return (T) new Float(data);
|
||||
else if(c == float.class) return (T) new Float(data);
|
||||
else if(c == Double.class) return (T) new Double(data);
|
||||
else if(c == double.class) return (T) new Double(data);
|
||||
else if(c == Boolean.class) return (T) new Boolean(data);
|
||||
else if(c == boolean.class) return (T) new Boolean(data);
|
||||
else if(c == Byte.class) return (T) new Byte(data);
|
||||
else if(c == byte.class) return (T) new Byte(data);
|
||||
else if(byte[].class.isAssignableFrom(c))
|
||||
return (T) Base64Decoder.decode(data);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces reserved and unsafe characters in URLs with hex values
|
||||
*/
|
||||
public static String urlEncode( String str ){
|
||||
StringBuilder out = new StringBuilder();
|
||||
for( char c : str.toCharArray() ){
|
||||
if( c>='0' && c<='9' || c>='A' && c<='Z' || c>='a' && c<='z' ||
|
||||
c=='$' || c=='-' || c=='_' || c=='.' || c=='+' || c=='!' ||
|
||||
c=='*' || c=='\'' || c=='(' || c==')' || c==',' )
|
||||
out.append( c );
|
||||
else{
|
||||
out.append( '%' ).append( toHexString((byte)c) );
|
||||
}
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces hex values from a URL with the proper characters
|
||||
*/
|
||||
public static String urlDecode( String str ){
|
||||
StringBuilder out = new StringBuilder();
|
||||
char[] array = str.toCharArray();
|
||||
for( int i=0; i<array.length ;i++ ){
|
||||
char c = array[i];
|
||||
if( c == '%' && i+2<array.length ){
|
||||
out.append( (char)hexToByte( array[++i], array[++i]) );
|
||||
}
|
||||
else
|
||||
out.append( c );
|
||||
}
|
||||
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
93
src/zutil/converter/ConverterTest.java
Executable file
93
src/zutil/converter/ConverterTest.java
Executable file
|
|
@ -0,0 +1,93 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.converter;
|
||||
|
||||
import org.junit.Test;
|
||||
import zutil.converter.Converter;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
public class ConverterTest {
|
||||
|
||||
@Test
|
||||
public void testHexToByte() {
|
||||
assertEquals( (byte)1, Converter.hexToByte('1') );
|
||||
assertEquals( (byte)5, Converter.hexToByte('5') );
|
||||
assertEquals( (byte)10, Converter.hexToByte('A') );
|
||||
assertEquals( (byte)10, Converter.hexToByte('a') );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexToByte2() {
|
||||
assertEquals( 0x00, Converter.hexToByte('0','0') );
|
||||
assertEquals( 0x11, Converter.hexToByte('1','1') );
|
||||
assertEquals( 0x75, Converter.hexToByte('7','5') );
|
||||
assertEquals( 0xDA, Converter.hexToByte('D','A') & 0xFF );
|
||||
assertEquals( 0xFA, Converter.hexToByte('F','a') & 0xFF );
|
||||
assertEquals( 0xFF, Converter.hexToByte('f','f') & 0xFF );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexStringToByte() {
|
||||
assertArrayEquals( null, Converter.hexToByte(null) );
|
||||
assertArrayEquals( new byte[]{}, Converter.hexToByte("") );
|
||||
assertArrayEquals( new byte[]{0x00}, Converter.hexToByte("0x00") );
|
||||
assertArrayEquals( new byte[]{0x00}, Converter.hexToByte("00") );
|
||||
assertArrayEquals(new byte[]{0x07,0x06,0x05,0x04,0x03,0x02,0x01},
|
||||
Converter.hexToByte("01020304050607") );
|
||||
assertArrayEquals( new byte[]{0x11,0x0F}, Converter.hexToByte("F11") );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlEncode() {
|
||||
assertEquals( "fas8dg7%20a0d1%2313f9g8d7%200h9a%25h0",
|
||||
Converter.urlEncode("fas8dg7 a0d1#13f9g8d7 0h9a%h0") );
|
||||
assertEquals( "9i34%202y9252%25%2623%20463765(%2f%26(",
|
||||
Converter.urlEncode("9i34 2y9252%&23 463765(/&(") );
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUrlDecode() {
|
||||
assertEquals( "fas8dg7 a0d1#13f9g8d7 0h9a%h0",
|
||||
Converter.urlDecode("fas8dg7%20a0d1%2313f9g8d7%200h9a%25h0") );
|
||||
assertEquals( "9i34 2y9252%&23 463765(/&(",
|
||||
Converter.urlDecode("9i34%202y9252%25%2623%20463765(%2f%26(") );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void byteArrayToInt(){
|
||||
assertEquals(0, Converter.toInt(new byte[]{}));
|
||||
assertEquals(1, Converter.toInt(new byte[]{0b0000_0001}));
|
||||
assertEquals(1, Converter.toInt(new byte[]{0x00,0x01}));
|
||||
assertEquals(256, Converter.toInt(new byte[]{0x00,0x01,0x00}));
|
||||
assertEquals(-1, Converter.toInt(new byte[]{(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF}));
|
||||
assertEquals(Integer.MAX_VALUE, Converter.toInt(new byte[]{(byte)0x7F,(byte)0xFF,(byte)0xFF,(byte)0xFF}));
|
||||
assertEquals(Integer.MAX_VALUE, Converter.toInt(new byte[]{(byte)0x7F,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF,(byte)0xFF}));
|
||||
}
|
||||
}
|
||||
116
src/zutil/converter/NumberToWordsConverter.java
Executable file
116
src/zutil/converter/NumberToWordsConverter.java
Executable file
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.converter;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class NumberToWordsConverter {
|
||||
|
||||
private static final String ZERO_STRINGS = "zero";
|
||||
// Indexes to lookup
|
||||
private static final int[] NUMERIC_INDEXES = new int[]{
|
||||
9,/*1000 000 000*/
|
||||
6,/*1000 000*/
|
||||
3,/*1000*/
|
||||
2 /*100*/};
|
||||
private static final HashMap<Long, String> NUMERIC_STRINGS;
|
||||
static{
|
||||
NUMERIC_STRINGS = new HashMap<>();
|
||||
NUMERIC_STRINGS.put(1l, "one");
|
||||
NUMERIC_STRINGS.put(2l, "two");
|
||||
NUMERIC_STRINGS.put(3l, "three");
|
||||
NUMERIC_STRINGS.put(4l, "four");
|
||||
NUMERIC_STRINGS.put(5l, "five");
|
||||
NUMERIC_STRINGS.put(6l, "six");
|
||||
NUMERIC_STRINGS.put(7l, "seven");
|
||||
NUMERIC_STRINGS.put(8l, "eight");
|
||||
NUMERIC_STRINGS.put(9l, "nine");
|
||||
NUMERIC_STRINGS.put(10l, "ten");
|
||||
NUMERIC_STRINGS.put(11l, "eleven");
|
||||
NUMERIC_STRINGS.put(12l, "twelve");
|
||||
NUMERIC_STRINGS.put(13l, "thirteen");
|
||||
NUMERIC_STRINGS.put(14l, "fourteen");
|
||||
NUMERIC_STRINGS.put(15l, "fifteen");
|
||||
NUMERIC_STRINGS.put(16l, "sixteen");
|
||||
NUMERIC_STRINGS.put(17l, "seventeen");
|
||||
NUMERIC_STRINGS.put(18l, "eightteen");
|
||||
NUMERIC_STRINGS.put(19l, "nineteen");
|
||||
|
||||
NUMERIC_STRINGS.put(20l, "twenty");
|
||||
NUMERIC_STRINGS.put(30l, "thirty");
|
||||
NUMERIC_STRINGS.put(40l, "forty");
|
||||
NUMERIC_STRINGS.put(50l, "fifty");
|
||||
NUMERIC_STRINGS.put(60l, "sixty");
|
||||
NUMERIC_STRINGS.put(70l, "seventy");
|
||||
NUMERIC_STRINGS.put(80l, "eighty");
|
||||
NUMERIC_STRINGS.put(90l, "ninety");
|
||||
|
||||
NUMERIC_STRINGS.put(100l, "hundred");
|
||||
NUMERIC_STRINGS.put(1_000l, "thousand");
|
||||
NUMERIC_STRINGS.put(1000_000l, "million");
|
||||
NUMERIC_STRINGS.put(1000_000_000l, "billion");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Given an integer in the range -20 to 20 will return a String with
|
||||
* that number converted to words. For example, an input of 15 results in
|
||||
* an output of "fifteen". An input of -4 returns "minus four".
|
||||
*
|
||||
* @param num a number to be converted to words.
|
||||
* @return the number as words.
|
||||
*/
|
||||
public String convert(int num) {
|
||||
if (num == 0)
|
||||
return ZERO_STRINGS;
|
||||
|
||||
long tmpNum = num;
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
if (tmpNum < 0){ // Negative number
|
||||
tmpNum *= -1;
|
||||
buffer.append("minus ");
|
||||
}
|
||||
|
||||
for(int i : NUMERIC_INDEXES){
|
||||
long pow = (int)Math.pow(10, i);
|
||||
if (tmpNum >= pow){
|
||||
long numberAtIndex = tmpNum/pow; // The number at position 3
|
||||
tmpNum -= numberAtIndex*pow;
|
||||
buffer.append( convert((int)numberAtIndex) ).append(" ");
|
||||
buffer.append( NUMERIC_STRINGS.get( pow ) ).append(" ");
|
||||
}
|
||||
}
|
||||
if (tmpNum >= 20){ // second number in the integer
|
||||
long numberAtIndex = ((tmpNum % 100)/10)*10; // The number at position 2
|
||||
tmpNum -= numberAtIndex;
|
||||
buffer.append( NUMERIC_STRINGS.get(numberAtIndex) ).append(" ");
|
||||
}
|
||||
if (NUMERIC_STRINGS.containsKey(tmpNum))
|
||||
buffer.append(NUMERIC_STRINGS.get(tmpNum));
|
||||
return buffer.toString().trim();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
96
src/zutil/converter/NumberToWordsConverterTest.java
Executable file
96
src/zutil/converter/NumberToWordsConverterTest.java
Executable file
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.converter;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import zutil.converter.NumberToWordsConverter;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class NumberToWordsConverterTest {
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> parameters() {
|
||||
Object[][] data = new Object[][] {
|
||||
{-77, "minus seventy seven"}
|
||||
, {-2, "minus two"}
|
||||
, {1, "one"}
|
||||
, {0, "zero"}
|
||||
, {7, "seven"}
|
||||
, {11, "eleven"}
|
||||
, {12, "twelve"}
|
||||
, {17, "seventeen"}
|
||||
, {20, "twenty"}
|
||||
, {21, "twenty one"}
|
||||
, {23, "twenty three"}
|
||||
, {25, "twenty five"}
|
||||
, {30, "thirty"}
|
||||
, {34, "thirty four"}
|
||||
, {50, "fifty"}
|
||||
, {70, "seventy"}
|
||||
, {100, "one hundred"}
|
||||
, {110, "one hundred ten"}
|
||||
, {131, "one hundred thirty one"}
|
||||
, {222, "two hundred twenty two"}
|
||||
, {1000, "one thousand"}
|
||||
, {10_000, "ten thousand"}
|
||||
, {100_000, "one hundred thousand"}
|
||||
, {1000_000, "one million"}
|
||||
, {10_000_000, "ten million"}
|
||||
, {Integer.MAX_VALUE, "two billion one hundred forty seven million four hundred eighty three thousand six hundred forty seven"}
|
||||
, {Integer.MIN_VALUE, "minus two billion one hundred forty seven million four hundred eighty three thousand six hundred forty eight"}
|
||||
};
|
||||
return Arrays.asList(data);
|
||||
}
|
||||
|
||||
|
||||
private int input;
|
||||
private String expected;
|
||||
|
||||
|
||||
public NumberToWordsConverterTest(int input, String expected) {
|
||||
this.input = input;
|
||||
this.expected = expected;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConvert() {
|
||||
assertThat(new NumberToWordsConverter().convert(input)
|
||||
, is(equalTo(expected)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
72
src/zutil/converter/WGS84Converter.java
Executable file
72
src/zutil/converter/WGS84Converter.java
Executable file
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2015 Ziver Koc
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package zutil.converter;
|
||||
|
||||
public class WGS84Converter {
|
||||
public static void main(String[] args){
|
||||
System.out.println(toWGS84Decimal("N 59<35> 47' 43\"")+" "+toWGS84Decimal(" E 17<31> 42' 55\""));
|
||||
System.out.println(toWGS84Decimal("55<EFBFBD> 0' 0\"")+" "+toWGS84Decimal("68<EFBFBD> 59' 59,999\""));
|
||||
System.out.println(toWGS84Decimal("55<EFBFBD> 0.001'")+" "+toWGS84Decimal("68<EFBFBD> 59.999'"));
|
||||
System.out.println(toWGS84Decimal("3444.0000S")+" "+toWGS84Decimal("13521.0000E"));
|
||||
System.out.println(toWGS84Decimal("-44.0001")+" "+toWGS84Decimal("521.0001"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an WGS84 coordinate to an WGS84 decimal coordinate
|
||||
*
|
||||
* @param coordinate is the coordinate to convert
|
||||
* @return the new coordinate in decimal degrees, returns 0 if conversions fails
|
||||
*/
|
||||
public static float toWGS84Decimal(String coordinate){
|
||||
float deg=0, min=0, sec=0, neg=1;
|
||||
coordinate = coordinate.trim().replaceAll(",", ".").toUpperCase();
|
||||
if(coordinate.contains("S") || coordinate.contains("W"))
|
||||
neg = -1;
|
||||
|
||||
// 55<EFBFBD> 0' 68<EFBFBD> 59,999 or 55<EFBFBD> 0' 0" 68<36> 59' 59,999"
|
||||
if(coordinate.matches("[NSWE ]? ?[0-9]{1,3}<7D> [0-9]{1,2}.?[0-9]*'[ 0-9.\\\"]*")){
|
||||
coordinate = coordinate.replaceAll("[NSEW<45>'\\\"]", "").trim();
|
||||
String[] tmp = coordinate.split(" ");
|
||||
deg = Float.parseFloat(tmp[0]);
|
||||
min = Float.parseFloat(tmp[1]);
|
||||
if(tmp.length > 2){
|
||||
sec = Float.parseFloat(tmp[2]);
|
||||
}
|
||||
}
|
||||
// 3444.0000S 13521.0000E
|
||||
else if(coordinate.matches("[0-9]{4,5}.[0-9]*[NSEW]{1}")){
|
||||
coordinate = coordinate.replaceAll("[NS EW]", "");
|
||||
float tmpf = Float.parseFloat(coordinate);
|
||||
deg = (int)(tmpf/100);
|
||||
min = tmpf-(deg*100);
|
||||
}
|
||||
// 55.0 68.99999
|
||||
else if(coordinate.matches("\\-?[0-9]{2,3}.[0-9]*")){
|
||||
return Float.parseFloat(coordinate);
|
||||
}
|
||||
|
||||
return neg*(deg + min/60 + sec/3600);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue