1 17 package org.alfresco.repo.search.impl.lucene.analysis; 18 19 40 public class NumericEncoder 41 { 42 45 46 static int INTEGER_SIGN_MASK = 0x80000000; 47 48 51 52 static long LONG_SIGN_MASK = 0x8000000000000000L; 53 54 57 58 static int FLOAT_SIGN_MASK = 0x80000000; 59 60 static int FLOAT_EXPONENT_MASK = 0x7F800000; 61 62 static int FLOAT_MANTISSA_MASK = 0x007FFFFF; 63 64 67 68 static long DOUBLE_SIGN_MASK = 0x8000000000000000L; 69 70 static long DOUBLE_EXPONENT_MASK = 0x7FF0000000000000L; 71 72 static long DOUBLE_MANTISSA_MASK = 0x000FFFFFFFFFFFFFL; 73 74 private NumericEncoder() 75 { 76 super(); 77 } 78 79 87 public static String encode(int intToEncode) 88 { 89 int replacement = intToEncode ^ INTEGER_SIGN_MASK; 90 return encodeToHex(replacement); 91 } 92 93 101 public static String encode(long longToEncode) 102 { 103 long replacement = longToEncode ^ LONG_SIGN_MASK; 104 return encodeToHex(replacement); 105 } 106 107 115 public static String encode(float floatToEncode) 116 { 117 int bits = Float.floatToIntBits(floatToEncode); 118 int sign = bits & FLOAT_SIGN_MASK; 119 int exponent = bits & FLOAT_EXPONENT_MASK; 120 int mantissa = bits & FLOAT_MANTISSA_MASK; 121 if (sign != 0) 122 { 123 exponent ^= FLOAT_EXPONENT_MASK; 124 mantissa ^= FLOAT_MANTISSA_MASK; 125 } 126 sign ^= FLOAT_SIGN_MASK; 127 int replacement = sign | exponent | mantissa; 128 return encodeToHex(replacement); 129 } 130 131 139 public static String encode(double doubleToEncode) 140 { 141 long bits = Double.doubleToLongBits(doubleToEncode); 142 long sign = bits & DOUBLE_SIGN_MASK; 143 long exponent = bits & DOUBLE_EXPONENT_MASK; 144 long mantissa = bits & DOUBLE_MANTISSA_MASK; 145 if (sign != 0) 146 { 147 exponent ^= DOUBLE_EXPONENT_MASK; 148 mantissa ^= DOUBLE_MANTISSA_MASK; 149 } 150 sign ^= DOUBLE_SIGN_MASK; 151 long replacement = sign | exponent | mantissa; 152 return encodeToHex(replacement); 153 } 154 155 private static String encodeToHex(int i) 156 { 157 char[] buf = new char[] { '0', '0', '0', '0', '0', '0', '0', '0' }; 158 int charPos = 8; 159 do 160 { 161 buf[--charPos] = DIGITS[i & MASK]; 162 i >>>= 4; 163 } 164 while (i != 0); 165 return new String (buf); 166 } 167 168 private static String encodeToHex(long l) 169 { 170 char[] buf = new char[] { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' }; 171 int charPos = 16; 172 do 173 { 174 buf[--charPos] = DIGITS[(int) l & MASK]; 175 l >>>= 4; 176 } 177 while (l != 0); 178 return new String (buf); 179 } 180 181 private static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 182 'f' }; 183 184 private static final int MASK = (1 << 4) - 1; 185 } 186 | Popular Tags |