Guest User

Untitled

a guest
May 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. public interface NfcNormalizer
  2. {
  3. public String normalize(String str);
  4. }
  5.  
  6. public class IdentityNfcNormalizer implements NfcNormalizer
  7. {
  8. public String normalize(String str)
  9. {
  10. return str;
  11. }
  12. }
  13.  
  14. public class JDK16NfcNormalizer implements NfcNormalizer
  15. {
  16. public String normalize(String str)
  17. {
  18. return Normalizer.normalize(str, Normalizer.Form.NFC);
  19. }
  20. }
  21.  
  22. NfcNormalizer normalizer;
  23. try
  24. {
  25. normalizer = Class.forName("JDK16NfcNormalizer").newInstance();
  26. }
  27. catch(Exception e)
  28. {
  29. normalizer = new IdentityNfcNormalizer();
  30. }
  31.  
  32. import java.io.*;
  33. import java.util.*;
  34.  
  35. /**
  36. * Masks direct use of select system methods to allow transparent use of facilities only
  37. * available in Java 5+ JVM.
  38. *
  39. * Threading Design : [ ] Single Threaded [x] Threadsafe [ ] Immutable [ ] Isolated
  40. */
  41.  
  42. public class SysUtil
  43. extends Object
  44. {
  45.  
  46. /** Package protected to allow subclass SysUtil_J5 to invoke it. */
  47. SysUtil() {
  48. super();
  49. }
  50.  
  51. // *****************************************************************************
  52. // INSTANCE METHODS - SUBCLASS OVERRIDE REQUIRED
  53. // *****************************************************************************
  54.  
  55. /** Package protected to allow subclass SysUtil_J5 to override it. */
  56. int availableProcessors() {
  57. return 1;
  58. }
  59.  
  60. /** Package protected to allow subclass SysUtil_J5 to override it. */
  61. long milliTime() {
  62. return System.currentTimeMillis();
  63. }
  64.  
  65. /** Package protected to allow subclass SysUtil_J5 to override it. */
  66. long nanoTime() {
  67. return (System.currentTimeMillis()*1000000L);
  68. }
  69.  
  70. // *****************************************************************************
  71. // STATIC PROPERTIES
  72. // *****************************************************************************
  73.  
  74. static private final SysUtil INSTANCE;
  75. static {
  76. SysUtil instance=null;
  77.  
  78. try { instance=(SysUtil)Class.forName("SysUtil_J5").newInstance(); } // can't use new SysUtil_J5() - compiler reports "class file has wrong version 49.0, should be 47.0"
  79. catch(Throwable thr) { instance=new SysUtil(); }
  80. INSTANCE=instance;
  81. }
  82.  
  83. // *****************************************************************************
  84. // STATIC METHODS
  85. // *****************************************************************************
  86.  
  87. /**
  88. * Returns the number of processors available to the Java virtual machine.
  89. * <p>
  90. * This value may change during a particular invocation of the virtual machine. Applications that are sensitive to the
  91. * number of available processors should therefore occasionally poll this property and adjust their resource usage
  92. * appropriately.
  93. */
  94. static public int getAvailableProcessors() {
  95. return INSTANCE.availableProcessors();
  96. }
  97.  
  98. /**
  99. * Returns the current time in milliseconds.
  100. * <p>
  101. * Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the
  102. * underlying operating system and may be larger. For example, many operating systems measure time in units of tens of
  103. * milliseconds.
  104. * <p>
  105. * See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time"
  106. * and coordinated universal time (UTC).
  107. * <p>
  108. * @return The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.
  109. */
  110. static public long getMilliTime() {
  111. return INSTANCE.milliTime();
  112. }
  113.  
  114. /**
  115. * Returns the current value of the most precise available system timer, in nanoseconds.
  116. * <p>
  117. * This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock
  118. * time. The value returned represents nanoseconds since some fixed but arbitrary time (perhaps in the future, so values
  119. * may be negative). This method provides nanosecond precision, but not necessarily nanosecond accuracy. No guarantees
  120. * are made about how frequently values change. Differences in successive calls that span greater than approximately 292
  121. * years (263 nanoseconds) will not accurately compute elapsed time due to numerical overflow.
  122. * <p>
  123. * For example, to measure how long some code takes to execute:
  124. * <p><pre>
  125. * long startTime = SysUtil.getNanoTime();
  126. * // ... the code being measured ...
  127. * long estimatedTime = SysUtil.getNanoTime() - startTime;
  128. * </pre>
  129. * <p>
  130. * @return The current value of the system timer, in nanoseconds.
  131. */
  132. static public long getNanoTime() {
  133. return INSTANCE.nanoTime();
  134. }
  135.  
  136. } // END PUBLIC CLASS
  137.  
  138. import java.util.*;
  139.  
  140. class SysUtil_J5
  141. extends SysUtil
  142. {
  143.  
  144. private final Runtime runtime;
  145.  
  146. SysUtil_J5() {
  147. super();
  148.  
  149. runtime=Runtime.getRuntime();
  150. }
  151.  
  152. // *****************************************************************************
  153. // INSTANCE METHODS
  154. // *****************************************************************************
  155.  
  156. int availableProcessors() {
  157. return runtime.availableProcessors();
  158. }
  159.  
  160. long milliTime() {
  161. return System.currentTimeMillis();
  162. }
  163.  
  164. long nanoTime() {
  165. return System.nanoTime();
  166. }
  167.  
  168. } // END PUBLIC CLASS
  169.  
  170. String str = "éèà";
  171. try {
  172. Class c = Class.forName("java.text.Normalizer");
  173. Class f = Class.forName("java.text.Normalizer$Form");
  174. Field ff = f.getField("NFD");
  175. Method m = c.getDeclaredMethod("normalize", new Class[]{java.lang.CharSequence.class,f});
  176. temp = (String) m.invoke(null, new Object[]{str,ff.get(null)});
  177. } catch (Throwable e) {
  178. System.err.println("Unsupported Normalisation method (jvm <1.6)");
  179. }
  180. System.out.println(temp+" should produce [eea]");
Add Comment
Please, Sign In to add comment