1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.heapdumper; 18 19 import java.text.Collator; 20 import java.util.Arrays; 21 import java.util.Locale; 22 import java.util.regex.Matcher; 23 import java.util.regex.Pattern; 24 25 /** 26 * An enumeration of actions for which we'd like to measure the effect on the post-GC heap. 27 */ 28 enum Actions implements Runnable { 29 30 /** 31 * Does nothing. Exists to measure the overhead inherent in the measurement system. 32 */ 33 NOOP { 34 @Override run()35 public void run() { 36 // noop! 37 } 38 }, 39 40 /** 41 * Uses a collator for the root locale to trivially sort some strings. 42 */ 43 COLLATOR_ROOT_LOCALE { 44 @Override run()45 public void run() { 46 useCollatorForLocale(Locale.ROOT); 47 } 48 }, 49 50 /** 51 * Uses a collator for the US English locale to trivially sort some strings. 52 */ 53 COLLATOR_EN_US_LOCALE { 54 @Override run()55 public void run() { 56 useCollatorForLocale(Locale.US); 57 } 58 }, 59 60 /** 61 * Uses a collator for the Korean locale to trivially sort some strings. 62 */ 63 COLLATOR_KOREAN_LOCALE { 64 @Override run()65 public void run() { 66 useCollatorForLocale(Locale.KOREAN); 67 } 68 }, 69 70 REGEX { 71 @Override run()72 public void run() { 73 final String sequence = "foo 123 bar baz"; 74 Pattern p = Pattern.compile("foo (\\d+) bar (\\w+)"); 75 Matcher m = p.matcher(sequence); 76 77 boolean found = m.find(); 78 boolean matches = m.matches(); 79 int groups = m.groupCount(); 80 String first = m.group(1); 81 String second = m.group(2); 82 boolean hitEnd = m.hitEnd(); 83 84 // Set region to prefix of the original sequence 85 m.region(0, "foo 123".length()); 86 boolean matchesPrefix = m.lookingAt(); 87 boolean requireEnd = m.requireEnd(); 88 89 m.useTransparentBounds(true); 90 boolean matchesPrefixTransparentBounds = m.lookingAt(); 91 92 m.useAnchoringBounds(true); 93 boolean matchesPrefixAnchoringBounds = m.lookingAt(); 94 } 95 } 96 97 ; 98 useCollatorForLocale(Locale locale)99 private static void useCollatorForLocale(Locale locale) { 100 String[] strings = { "caff", "café", "cafe", "안녕", "잘 가" }; 101 Collator collator = Collator.getInstance(locale); 102 Arrays.sort(strings, collator); 103 } 104 } 105