guava
ãã¤ã®ã¾ã«ãããgoogle-collectionsãguavaã«çµ±åããã¦ãããç¾æç¹ã§ã®ãã¼ã¸ã§ã³ã¯r06ãguavaã¯Googleã®å¤ãã®Javaãã¼ã¹ã®ããã¸ã§ã¯ãã§ä½¿ç¨ããã¦ããjava core libraryã¨ã®ãã¨ã
ããã±ã¼ã¸åãè¦ãã°å¤§ä½ã®æ©è½ã«ã¤ãã¦æ³åãã¤ãã
- com.google.common.annotations
- com.google.common.base
- com.google.common.collect
- com.google.common.io
- com.google.common.net
- com.google.common.primitives
- com.google.common.util.concurrent
è¦ã¦ããæ°ä»ãã®ã¯commons-langãcommons-collectionsãªã©commonsç³»ã¨æ©è½ãéè¤ãã¦ããã¨ãããã¨ãéãã¨ããã°ã以ä¸ã®ãããªç¹ã§ããããã
- commons-collectionsã®ææ°çã¯3.2.1ã ã2008/04ã§éçºãæ¢ã¾ã£ã¦ããã£ã½ããguavaã¯ä»ãéçºãç¶ãã¦ããã
- ã¸ã§ããªã¯ã¹ããµãã¼ãï¼ããã«1.5以ä¸ãå¿ è¦ï¼
- fluent interfaceãã¼ã¹ã®ããã°ã©ãã³ã°
- 1ã¤ã®jarã§ä¸è¨ããã±ã¼ã¸ãã«ãã¼ãã¦ããã®ã§ãlib以ä¸ãæ±ããªã
guavaã®æ¹ãä»ã£ã½ãJavaããã°ã©ãã³ã°ãå¯è½ã«ãªãã¾ãããã©commonsã¨æ¯ã¹ãã¨æ©è½ã¯å°ãªãã¨æãã¾ãã®ã§è©³ããã¯javadocãèªãã§ç¢ºèªãã¦ããå¿ è¦ãããã¨æãã¾ãã
ã¨ããããå人çã«netã¨concurrentã¯ä½¿ç¨é »åº¦ãå°ãªãããªã®ã§ä»¥ä¸ã§ã¯çç¥ã
com.google.common.annotations
4ã¤æä¾ããã¦ãã¾ãããæ®æ®µä½¿ãã§ããããªã®ã¯1ã¤ããªã
VisibleForTestingã¯ãã¡ã½ããããã£ã¼ã«ãã«ä»ä¸ãããã®ã§ããã¹ããããããããããã«ã¢ã¯ã»ã¹ä¿®é£¾åã®ã¬ãã«ãç·©ããã¦ããã¨ãããã¨ãæ示ããããã«ä»ä¸ããããã¹ãã®ããã«privateãããã±ã¼ã¸ãã©ã¤ãã¼ãã«å¤æ´ãããã£ã¦ãã¨ã¯ããããã®ã§ããã®ã¢ããã¼ã·ã§ã³ãå©ç¨ããã®ã¯ããããã
com.google.common.primitives
BooleansãBytesãCharsãDoublesãããã¨ããã決ã¾ãã®ããªããã£ãåã«å¯¾ããã¦ã¼ãã£ãªãã£ã¡ã½ãããæä¾ããã¯ã©ã¹ãå«ã¾ãã¦ãã¾ããbyteã«é¢ãã¦ã¯ãBytesãSignedBytesãUnsignedBytesãªãããããã¾ãããç°¡åãªä¾ã以ä¸ã«ã
int[] ary = new int[] { 1, 2, 3 }; List<Integer> list = Ints.asList(ary); // é åããListã¸ã®å¤æ int max = Ints.max(ary); // 3 String joinedStr = Ints.join(" : ", ary); // 1 : 2 : 3
com.google.common.base
åºç¤ã¨ãªãã¦ã¼ãã£ãªãã£ã¯ã©ã¹ãå«ã¾ããããã±ã¼ã¸ã大ããåãã¦ããStringé¢é£ãã¨ãFunctionalåãã¨ããã®ä»ãã®ï¼ã¤ã«åé¡ãããï¼javadocããï¼ã
â Stringé¢é£
Stringsã«ã¯ãããªã®ãããã¾ãã
if ( Strings.isNullOrEmpty(str); ) {
ã¨ã
String repeat = Strings.repeat("*", 10); // **********
ã¨ãã
ä»ã«ã¯ããããªã®ãã
String str1 = Strings.padStart("7", 2, '0'); // 07 String str2 = Strings.padStart("12", 2, '0'); // 12
第1å¼æ°ã®æååãæä½ä½æåã§ããå¿
è¦ããããã¨ããã®ã第2å¼æ°ã§æå®ãã¦ããããæºããã¦ããªãæã¯ç¬¬3å¼æ°ã®æåã§æåãåããã¨ãããã®ã
ããã¨å¯¾ã¨ãªããStrings#padEndãããã¾ãã
Joinerã¨ãããã¼ã¿ãåºåãæåã§é£çµãã¦ä¸ã¤ã®æååã«ããã¯ã©ã¹ããããå¼æ°ã¯ãªã¹ãã§ãé åã§ãOKï¼
String[] animals = new String[]{"dog", "cat", null, "pig"}; String str = Joiner.on(", ").skipNulls().join(animals); // dog, cat, pig
Mapã ã£ã¦ããã¡ããã¾ãã
Map<String, String> dictionary = new HashMap<String, String>(); dictionary.put("ç±³", "rice"); dictionary.put("ãã³", "bread"); dictionary.put("ãã©ã", null); MapJoiner joiner = Joiner.on(", ").withKeyValueSeparator(":").useForNull("ç»é²ãªã"); String str = joiner.join(dictionary); // ç±³:rice, ãã³:bread, ãã©ã:ç»é²ãªã
Splitterã£ã¦ã®ãããã¾ããCharMatcherï¼ãããbaseããã±ã¼ã¸ã®æååç³»ã¯ã©ã¹ï¼ã¨ã®åããæã§ã
String telnum = "090 1234 5678"; // ãããã¯090-1234-5678ã«å¯¾å¿ CharMatcher matcher = CharMatcher.WHITESPACE.or(CharMatcher.is('-')); Iterable<String> splits = Splitter.on(matcher).split(telnum); // 090,1234,5678ã¨ããé·ã3ã®ãªã¹ã
onã®å¼æ°ã®ã¨ããã¯CharMatcherã ããããªãã¦æ£è¦è¡¨ç¾ãã¿ã¼ã³ãªããã使ãã¾ããããCharMatcherã使ã£ãã»ããå¯èªæ§ã¯ä¸ããã¨æãã¾ããæ£è¦è¡¨ç¾ã¯è¤éã«ãªãã¨åªæã®ããã«ãªãã®ã§ã
ã¾ããString#splitã使ãã¨ä¸è¨ä¾ã¯ãã®ããã«ãªãã¾ãã
String[] split = telnum.split("\\s|-");
ã·ã³ãã«ãªæã¯ããã§ååã ã¨æãã¾ããã§ããã®ä¾ã§ãã£ã¦ãä¾ãã°ä¸è¨ä¾ã®telnumãã090 1234 5678ãã¨ã¹ãã¼ã¹ã1ã¤ãããªãã¦3ã¤é£ç¶ãã¦ãã¾ãã¨ãé·ãã3ã§ã¯ãªãé·ã5ã®é åã«ãªã£ã¦ãã¾ãã¾ãããããªæãSplitterã«ä¸ã¤è¨å®ãå ãã¦ããã¨ä½è¨ãªã¹ãã¼ã¹ãåãé¤ãã¾ãã
Iterable<String> splits = Splitter.on(matcher).omitEmptyStrings().split(telnum); // 090,1234,5678ã¨ããé·ã3ã®ãªã¹ã
â Functionç³»
åç¬ã§èª¬æãã¦ãåããã«ãããcom.google.common.collectã¨çµã¿åããããã¨ã§åããããããªãã®ã§ããã¨ã§ã
â ãã®ä»
Objectsã£ã¦ã®ãããã¾ããhashCodeã®å®è£
ãtoStringã®å®è£
ã«ä¾¿å©ãªã¡ã½ãããããã¾ãã
public int hashCode() { return Objects.hashCode(getX(), getY(), getZ()); } public String toString() { return Objects.toStringHelper(this).add("x", x).add("y", y).toString(); }
Preconditionsã使ããã¨ã§é²å¾¡çããã°ã©ãã³ã°ãä¿é²ãã¾ãã
public void setRating(StarRating rating) { this.rating = checkNotNull(rating, "å¼æ°ã«nullã¯è¨å®ã§ãã¾ãã[rating]"); // NullPointerExceptionãthrow }
com.google.common.collect
æ§google collections libraryãæ¨æºã®JDKã«ã¯å«ã¾ãã¦ããªãcollectionã®å®è£ ã¯ã©ã¹ãã¾ã¨ããã
ã¯ã©ã¹å | ç¹å¾´ |
MultiSet | åãå¤ãæã¤ãã¨ãã§ããSet |
MultiMap | ãã¼ã«å¯¾ããå¤ãè¤æ°ãã¤ãã¨ãã§ããmapãå¤ã®ç®¡çã®ä»æ¹ã§ListMultiMapã¨SetMultiMapãé¸æã§ããã |
BiMap | ãã¼ã ãã§ãªãå¤ãã¦ãã¼ã¯ããä¿è¨¼ããmapãBiMap#inverseã§ããã¼ã¨å¤ãå転ããã |
ç¹ã«MultiMapã¯å¤ç¨ãããã§ããkeyãå«ã¾ãã¦ããªããã°ããã¨ãããã§ãã¯ããããªãããåã«putããã ãã§Collectionã«è¿½å ããããã以ä¸ä¾ã
// æ§æ¥ if (!map.contains(k)) { map.put(k, new ArrayList()); } map.get(k).add(v); // MultiMap map.put(k, v);
ãªãã宣è¨ã¯ãããªæãã
private Multimap<String, String> info = ArrayListMultimap.create();
ãã¨ã¯Collectionç³»ã®å®£è¨é¨åãç°¡ç´ åããã¦ãã¦ããæãã§ãã
List<String> l1 = Lists.newArrayList(); // ã¸ã§ããªã¯ã¹å®£è¨ã¯å宣è¨æã®1åã ã List<String> l2 = Lists.newArrayList("dog", "cat", "pig"); // é åã¿ããã«åã¨åæå¤ãåæã«å®£è¨ã§ããã final List<String> l3 = ImmutableList.of("dog", "cat", "pig"); // åæå¤ãè¨å®ãããªãå¤åimmutableãªå ´åãå¤ãã®ã§ã¯ããããªæã¯ããã final Map<String, String> m1 = ImmutableMap.of("dog", "ç¬", "cat", "ç«"); // Mapãåã¨åæå¤ãåæã«å®£è¨ã§ãããã¤immutableã«ãk,v,k,v...
ã§ã¯ããã®è¾ºã§å
ã®baseããã±ã¼ã¸ã§é£ã°ããFunctionç³»ãããã¾ãã
â Predicate
è±è¾éã§è¨³ãã¦ã¿ããè¿°é¨ãè¿°èªã¨ããæå³ããããæ°æã¡ã¯åãããã©ã¡ãã£ã¨åããã«ãããªãã³ã¼ãè¦ãã»ããæ©ãã
ã¤ã³ã¿ãã§ã¼ã¹ã¯ã·ã³ãã«ã§ãå
¥åå¤ã«å¯¾ãã¦trueãfalseãè¿ãå®è£
ããã°ããã
boolean apply(T input)
ã¨ãã£ã¦ãã¾ã åããã«ããã§ãããCollectionã¨ã®çµã¿åããä¾ãã
List<Integer> list = Ints.asList(1,2,3,4,5); List<Integer> evenList = (List<Integer>)Collections2.filter(list, new Predicate<Integer>() { @Override public boolean apply(Integer input) { return input % 2 == 0; } });
filteringããããã®æ¡ä»¶é¨ãæ§æãããã§ãããã ããè¿°é¨ã£ã¦ããæå³ãªãã§ããããã
Predicateã¯ç¡åã¯ã©ã¹ã«ããããã¡ããã¨ã¯ã©ã¹å®ç¾©ãã¦å®æ°ã¨ãã«ãã¦ãã¾ãæ¹ãå¯èªæ§ãä¸ããã¾ããã
List<Integer> evenList = (List<Integer>)Collections2.filter(list, EVEN_PREDICATE);
â Function
å®è£
ããªãã¨ãããªãã¡ã½ããã¯ãããªæããinputã«å¯¾ãã¦ä½ãããã®å¦çããã¦è¿ãã
T apply(F input)
Collectionã¨ã®çµã¿åããä¾ãã
List<Integer> list = Ints.asList(1,2,3,4,5); List<Integer> negativeList = Lists.transform(list, new Function<Integer, Integer>() { // -1,-2,-3,-4,-5 @Override public Integer apply(Integer input) { return -1 * input; } });
Predicateã ãã§ãªãFunctionãç¡åã¯ã©ã¹ã¨ãã¦å®£è¨ããããã¯ã¡ããã¨ã¯ã©ã¹å®ç¾©ãã¦åå©ç¨ãä¿ãæ¹åã§ä½¿ãã®ãããã¨æãã¾ãã
com.google.common.io
â Files
BufferedReaderã®ä½æãæ¯è¼ç容æã
// æ§æ¥ BufferedReader reader = new BufferedReader(new FileReader(getFile())); // Files BufferedReader reader = Files.newReader(getFile(), Charsets.UTF_8);
ãã¨ã¯è¡é¢é£ã
ãã¹ã¦ã®è¡ãListã§åå¾ããã®å¾ãforeachã§èªåã§å¦çããã¿ã¤ãã
List<String> lines = Files.readLines(getFile(), Charsets.UTF_8);
ãã£ã¡ã¯å¦çãåæã«ãã£ã¡ããããaãããå§ã¾ãè¡ã®ã¿ãæ½åºãã¦è¿ããprocessLineã§falseãè¿ãã¨ãã®è¡ã§å¦çãçµäºããããã®ä¾ã§ã¯trueã«ãã¦ããã®ã§ãåããããå ¨è¡å¦çããã
String lines = Files.readLines(getFile(), Charsets.UTF_8, new LineProcessor<String>() { private List<String> lines = Lists.newArrayList(); @Override public boolean processLine(String line) throws IOException { if(line.startsWith("a")) { lines.add(line); } return true; } @Override public String getResult() { return Joiner.on(",").join(lines); } });
ãã¨ã¯copyã¨ãmoveã¨ãtouchã¨ãUnixã©ã¤ã¯ãªã¡ã½ãããããã¾ãã
â Resources
// æ§æ¥ URL resource = this.getClass().getClassLoader().getResource("sample.txt"); // Resources URL resource = Resources.getResource("sample.txt");
åãããã以ä¸ã§çµäºã§ãã