ç´°ãããã¦ä¼ãããªãJava7ã®å¤æ´ç¹
æ¬æ¥ã®ã¨ã³ããªã¼ã¯Java Advent Calendarã®25æ¥ç®ã§ããæ¨æ¥ã¯ @mike_neck ããã®mike、mikeなるままに…: hamcrestを拡張してmoreThanとか作ってみたã§ãããæ¬æ¥ã¯ã¯ãªã¹ãã¹ã§ãããAdvent Calendarã¯ã¾ã ã¾ã ç¶ãã¾ããææ¥ã¯t.ogisawaããã®http://webinter.sakura.ne.jp/pbd/210ã§ãã
ãã¦ãä»å¹´ã¯Java7ããªãªã¼ã¹ããã¾ããããtry-with-resourcesãdiamond operatorãinvoke dynamicãFolk/Join frameworkâ¦é åçãªæ°æ©è½ã®æ°ã ããå¤ãã®äººã«ãã£ã¦ç´¹ä»ããã¦ãã¾ããããJava7ã®å¤æ´ã¯ããã ããããªãã¯ããå°ç²ã ãããã©ãªã¨å ãå¤æ´ããã£ã¨ããã¯ããã¨ãããã¨ã§ãJava6ã¨Java7ã®src.zipãæ¯è¼ãã¦ã¿ã¾ãããå°ãã¿ãªã®ã§ãµã©ã£ã¨èªãã§é ããã°ã¨æãã¾ãã
ã¾ããæ¯è¼ã«ç¨ããã®ã¯ä»¥ä¸ã®éãã
ã§ãã¾ãæ®éã«diffãæãã¦ã¿ãã®ã ããæå¤ã¨docã³ã¡ã³ãä¸ã§é å¼µã£ã¦ãããã¨ãå¤æãdocã³ã¡ã³ãå
ã®codeã¿ã°ãttã¿ã°ãpreã¿ã°ã®å¤ãã{@code}ã{@link}ã§æ¸ãç´ãã¦ããé¨åãç®ç«ã¤ãã¾ããæªã ã«codeã¿ã°ã®ã¾ã¾ã®é¨åãå¤ãã®ã ããé å¼µã£ããããå人çã«ã¯HTMLã¿ã°ãããèªã¿ãããã¨æã£ã¦ããã®ã§ãå¬ããã§ãã
ãã¨ãJava6ã¾ã§ã®ã½ã¼ã¹ã¯ãã¤ã³ãã³ããTABã ã£ããSPACEã ã£ããã¾ã¡ã¾ã¡ã ã£ããããã¦ã©ãããTABå¹ 8ã¨ãã¦ããããã§ãTABå¹ 4ã®ç°å¢ã§è¦ãã¨ããã¯ããã¯æ®å¿µãªæãã«ãªã£ã¦ãã¾ãããããã®è¾ºãã®ã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ã«é¢ãã¦ã¯ãæ¨æºAPIã§ããã«ãé¢ãããããå£æ» çã§ãããããèªåã®ã³ã¼ãã£ã³ã°è¦ç´ãJavadocã®æ¸ãæ¹ã«ã¼ã«çå®ã®åèã«ãããã¨æã£ã¦æç¶ã¨ããè¨æ¶ããããããããJava7ã§ã¯ãã¤ã³ãã³ããSPACEã«ç´ã£ã¦ããã®ãç®ç«ã¤ãæªç¢ºèªã ããå ¨é¨çµ±ä¸ããã¦ããã®ãããããªããåã°ããã
ã¨ãã£ãæã®å·®ç°ã¾ã§diffã§è¦ãã¦ãã¾ãã¨ããã¯ããã¤ãºã§ãããªãã®ã§ãããã¯æãåã£ã¦ä¸¡è ã®ã½ã¼ã¹ãããã£ã¦ãã¾ãã¾ãã
find . -type f | xargs sed -e 's/\<code\>\([^<]*\)\<\/code\>/{@code \1}/g' -i "" find . -type f | xargs sed -e 's/\<tt\>\([^<]*\)\<\/tt\>/{@code \1}/g' -i ""
両è ã®ã½ã¼ã¹ã«å¯¾ãã¦ãä¸è¨ã®ç½®æãæããå¾ã俺俺ã³ã¼ããã©ã¼ããã¿ãæãã¦ããdiffã«æã¿ã¾ãããã§ãcom.sunããã±ã¼ã¸ãªããã®å·®ãè¦å§ãã¦ãã¢ã¬ãªã®ã§ãã²ã¨ã¾ã java.* ããã±ã¼ã¸ã«çµã£ã¦ãããã¦é½å ãå人çã«æ°ã«ãªã£ããã¤ã³ããä¸å¿ã«ã以ä¸ã«ãç´¹ä»ãã¾ã¼ãã
ã¹ãã«ãã¹ãç´°ãããã°ãã£ãã¯ã¹
ã¾ãã¯è»½ããJava6ã¨7ã®æ¯è¼ãã¨ããè¦ç¹ã§ã¯ããã¾ããããçµæ§è²ã ç´ãã¦ãã¾ããã
* @throw new NullPointerException
ãªãã¦ããdocã³ã¡ã³ãããã£ãããå¢ãã§newã£ã¦æ¸ãã¡ãã£ããã ãããªã¼ï½
public Foo { // ⦠private void Foo() {} }
ãªãã¦ãã£ã¡ãã£ã¦ãã¯ã©ã¹ãããã¾ããã
å¤ãã³ã¼ãã£ã³ã°ã¹ã¿ã¤ã«ã®å·æ°
Java6ã®ã½ã¼ã¹ã«ã¯static publicã¨ããèªé ï¼ï¼ï¼ããchar foo[]; ã®ãããªé
å宣è¨ãrawtypeåãªã©ãå¤ãæ¸ãæ¹ãå
é¨ã«éåæ®ã£ã¦ã¾ãããããã£ãæãã¡ããã¡ããç´ãã¦ããã¾ããããã¨ããã¤ã¢ã¢ã³ããªãã¬ã¼ã¿ããã£ã¡ã使ããã¦ã¾ãããã
åExceptionå®è£ ã¯ã©ã¹ã«serialVersionUIDã追å
Exceptionã¯Serializableã®ãµãã¿ã¤ãã§ããå¾ã£ã¦ãä¾å¤ã®å®è£
ã¯ã©ã¹ã«ã¯å
¨ã¦serialVersionUIDãè¨è¿°ããã®ãæã¾ãããã§ãããã¾ããå®è£
ã®è©³ç´°ã®è©±ã§ã¯ããã¾ãããåExceptionã«serialVersionUIDã追å ããã¦ã¾ãã
Byte/Integer/Long/BigIntegerã®æååpaese
int i1 = Integer.parseInt("3"); int i2 = Integer.parseInt("-6"); int i3 = Integer.parseInt("+2");
çããããã®ã³ã¼ãå®è¡ããã¨ã©ããªãã¨æãã¾ãï¼ å®ã¯ãJava6ã 㨠"+2" 㯠NumberFormatException ã«ãªã£ã¦ãã¾ãã®ã§ããæååã®æ´æ°parseã«ããã¦ããã©ã¹è¨å·ã¯ä»ã¾ã§ä½¿ãã¾ããã§ãããããããJava7ããã¯æ®éã«éãããã«ãªãã¾ãã
ã¡ãªã¿ã«ãDoubleãFloatã®parseã§ã¯ãJava6ã§ããã©ã¹è¨å·ã使ãã¾ãã
primitive wrapper class
Integer.compare(10, 15);
ã¨ãããããªãæ¯è¼ãã¸ãã¯ãstaticã¡ã½ããã¨ãã¦æä¾ãããããã«ãªãã¾ãããããªããã£ãã©ããã¼åã«ããããå®ç¾©ããã¦ãã¾ããæ¯è¼ãã¸ãã¯ãå¼ãç®ã§å®è£ ãã¦ãã°ãåºãã¦ãã¾ãä½ãªãããã®ã¦ã¼ãã£ãªãã£ã¡ã½ããã«å§è²ãã¦ãã¾ãã®ãããã§ããã
æ¯è¼ãã¸ãã¯ã®å¼ãç®å®è£ ã«ã¤ãã¦ã¯ä¸è¨åèæ¸ç±ã®ãããºã«65ããåç §ã
Java Puzzlers ç½ ãè½ã¨ãç©´ãã³ã¼ãã¼ã±ã¼ã¹
- ä½è : ã¸ã§ã·ã¥ã¢ã»ãããã¯,ãã¼ã«ã»ã¬ãã¿ã¼,æ´ç°è³æ¨¹
- åºç社/ã¡ã¼ã«ã¼: ãã¢ã½ã³ã»ã¨ãã¥ã±ã¼ã·ã§ã³
- çºå£²æ¥: 2005/11/14
- ã¡ãã£ã¢: 大åæ¬
- è³¼å ¥: 3人 ã¯ãªãã¯: 92å
- ãã®ååãå«ãããã° (56件) ãè¦ã
Comparableã¨Comparator
å¾æ¥ãComparable#compareToãComparator#compareã®å¼æ°ã«nullã渡ããæã®ä»æ§ã¯ãæªå®ç¾©ãã§ãããã¾ããç§ã¯åºåºåã®javadocã«æ示ãã¦ããä¾å¤ããæããªãããã«ãã¦ããããããnullã¨ã¯æ¯è¼ã§ããªãComparableããå®è£ ãããæãã¨ã¦ãæ°æã¡æªãæãããã¦ãã¾ããã
ãããªä¸ãJava7ã§ã¯ãããã®æ¯è¼ã¡ã½ããã®javadocã«ãNPEã®è¨è¿°ã追å ããã¾ããã
@throws NullPointerException if the specified object is null
å¼æ°ãnullãªæã¯NPEãæãã¦ãããªã£ãã®ã§ããã
Collections
以åãããCollectionsã¯ã©ã¹ã«ã¯ emptyList() çã®ã¡ã½ãããããã¾ããããä¼¼ããããªæãã§ä»¥ä¸ã®ã¡ã½ããã追å ã«ãªãã¾ããã大ãããã¨ã§ã¯ããã¾ãããã使ãæ©ä¼ãããã°ä½¿ã£ãæ¹ãè¯ãã§ããã
- emptyEnumeration()
- emptyIterator()
- emptyListIterator()
Objets
æ°ããã¦ã¼ãã£ãªãã£ã¯ã©ã¹ã§ããrequireNonNullãnull-safeãªequals/hashCode/toStringãªã©ãå°ç²ãªãã使ãåæã¯è¯ãããã§ãã
ReflectiveOperationException
å¾æ¥ããªãã¬ã¯ã·ã§ã³APIãå©ç¨ãããã¨ããã¨æ°å¤ãã®ãã§ãã¯ä¾å¤ãå¦çããå¿
è¦ãããã¾ãããNoSuchMethodException, InvocationTargetException, ClassNotFoundException, IllegalAccessException⦠å®éã¯çºçããªãã¨è¸ãã§ããä¾å¤ããããªã«å¤§éã«ãã£ãããããããã®ã¯ã¹ãã¬ã¹ã§ããããããããJava7ããã¯ReflectiveOperationExceptionã¨ããåºåºã¯ã©ã¹ãå®ç¾©ããã¾ãããããã«ãã£ã¦ããªãã¬ã¯ã·ã§ã³æä½æã®ä¾å¤ãã¾ã¨ãã¦ãã£ããã§ãã¾ãã
AutoCloseable
try-catch-resourcesç¨ã«åºã¦æ¥ãæ°ããã¤ã³ã¿ã¼ãã§ã¤ã¹ã§ããCloseableã¯ãã¡ãããConnection/ResultSet/Statementãããã¤ã®ãµãã¿ã¤ãã«ã
å¾æ¥ã¯JDBC APIç¨ã® closeQuietly çãªã¦ã¼ãã£ãªãã£ããI/O ã¨ã¯å¥ã«æ¸ããªããã°ãªãã¾ããã§ããããä»åº¦ããã¾ã¨ãããã¾ããã
ThreadLocalRandom
ã£ã¦ããã¯ã©ã¹ã追å ã«ãªã£ã¦ãã¾ããã¾ããå
容ã¯ååããæ³åã§ããéãã§ãã
Scanner
ScannerãCloseableã®ãµãã¿ã¤ãã«ãªãã¾ãããä»ã¾ã§Closeableãããªãã£ãã®ã¯ãåã«å¿ãã¦ãã ããªã®ã§ããããï½
Integer, Short, Longã®valueOf
Sun(Oracle)ã®Javaã«ããã¦ãIntegeråã®ã¤ã³ã¹ã¿ã³ã¹ã¯-128ã127ã®å¤ããã£ãã·ã¥ããããã¨ããã®ã¯æåãªï¼ï¼ï¼è©±ã§ããããããããã®ãã£ãã·ã¥ã¯ãSun(Oracle)ã®å®è£ ããã¾ãã¾ãããªã£ã¦ãããã ãã§ãããJavaã®ä»æ§ã§ã¯ãªããã®ã§ãããã¤ã¾ããå¥ã®Javaå®è£ ï¼ä¾ãã°IBM Javaã¨ãï¼ï¼ã§ã¯ãã£ãã·ã¥ããã¦ããªãããããã¾ããããã£ãã·ã¥ã®ç¯å²ãéãããããã¾ããã
ã¨ããç¶æ³ã ã£ãã®ã§ãããJava7ã®javadocã«ã¯ä»¥ä¸ã®ãããªè¨è¿°ã追å ããã¦ãã¾ãã
ãInteger#valueOfåã³Short#valueOfããã * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range.
ãLong#valueOfããã * Note that unlike the {@linkplain Integer#valueOf(int) * corresponding method} in the {@code Integer} class, this method * is <em>not</em> required to cache values within a particular * range.
ããã£ã¦â¦ãJavadocã«æ示ãããã¨ãããã¨ã¯ããã£ãã·ã¥ã®æåãå«ãã¦ä»æ§åããã¨ãããã¨ã§è¯ããã§ãããããã
System
System#lineSeparator() ã£ã¦ããã¡ã½ããã追å ããã©ãããã©ã¼ã ä¾åã®æ¹è¡æåï¼åï¼ãããã£ã¨æã«å ¥ãããã¦ä¾¿å©ã§ããã
ã¾ããSystemã¯ã©ã¹å
ã® in, out staticãã£ã¼ã«ãã®åæåæ¹æ³ãå¤ãã£ãããã§ããJava6ã¾ã§ã¯ãstaticãã£ã¼ã«ããåç
§å
ã¯ã©ã¹ã«ã¤ã³ã©ã¤ã³å±éãããªãããã«ãã¡ãã£ã¨ããããã¯ãããã¦ãã¾ããããJava7ã§ã¯ç´æ¥nullåæåãã¦ãã¾ããããã¯ã¤ã³ã©ã¤ã³å±éãããªããªã£ããã§ããããããã¾ã§ã¯è¿½ãããã¾ããã§ããã
æ¦ã·ã¹ãã é¢é£
week yearã®ãµãã¼ããæåããªã£ã¦ã¾ããGregorianCalendarã«ä»¥ä¸ã®3ã¡ã½ããã追å ã«ãªãã¾ããã
- getWeeksInWeekYear()
- getWeekYear()
- isWeekDateSupported()
ã£ã¦ä½ã®äºã ãããããã§ããããã¼ã¨ãã1é±éã¯ææãæ¥æã§ãããã¨ãã¦ãã2011å¹´ã®ç¬¬1é±ãã£ã¦ä½æ¥ããä½æ¥ã ã¨æãã¾ããï¼ ã¾ããã®ãã¿ã¯ãã¡ãã詳ããã®ã§åç
§ãã¦ãã ããã â 今が年の何週目か - 気付いたとき、気が向いたとき。by ykhr
ã§ãçµè«ã¨ãã¦ã¯ãISO8601çã«ã¯1/3ã1/9ãã2011å¹´ã®ç¬¬1é±ãã¨ãããã¨ã«ãªãã¾ããã¨ãããã¨ã¯ã2010/12/27ã2011/01/02ã¯ã2010å¹´ã®ç¬¬52é±ããªãã§ãããããã©ã«ãã®GregorianCalendarã®æåã¯ã1é±éã¯æ¥æå§ã¾ãã§ã1/1ãå«ãã®ã第1é±ãã¨ãããã¨ã«ã«ãªã£ã¦ãã¾ããããããISOçã«ã1é±éã¯ææå§ã¾ãã§ã1/4ãå«ãã®ã第1é±ãã¨ããè¨å®ãããã®ãå ã»ã©ã® id:ykhr-kokko æ°ã®ã¨ã³ããªã§ãã
ã§ãããã§æ³¨ç®ãããã®ã¯ã2011/01/02ã¯ã2011å¹´ã®æ¥ä»ã§ããã«ãé¢ããããé±ã¬ãã«ã§è¦ãã¨2010å¹´ã«å±ãããã¨ãããã¨ã§ããããã©ãããã§ããï½ ä»æ¥ã¯ä½å¹´ã®ç¬¬ä½é±ãªã®ããç¥ãããã¨ãã¾ãã第ä½é±ã㯠cal.get(Calendar.WEEK_OF_YEAR) ã§è¯ãã§ããããã§ã¯ãããæ¥ä»ãä¸ããæãããã¯é±ã¬ãã«ã§ã¯ä½å¹´ã«å±ããã®ãï¼ããã©ããã£ã¦åãã®ããããã getWeekYear() ã§ãã
ã¨ã¯è¨ããå¤§æµ Calendar.YEAR ã¨ä¸è´ãã¦ã¾ããããå¹´æ«å¹´å§ã ãæ°ãã¤ãã¦ããã°ç°¡åã«è¨ç®ã§ããããããï¼ â¦ã¨æããããæ£ç¢ºã«ããåºãã¨çµæ§å¤§å¤ã¿ããã§ãã1582年以åã¯ãä»ã®ã°ã¬ã´ãªãªæ¦ã§ã¯ãªãã¦ãªã¦ã¹æ¦ãâ¦è©³ãã㯠getWeekYear() ã®å®è£ ãã覧ä¸ããã
ãã§ã¾ããä¸è¨ã«é¢é£ãã¦SimpleDateFormatã«æ°ãããã¿ã¼ã³ã追å ã«ãªãã¾ãããY, u, X ã®3ã¤ã§ããããã®Yã£ã¦ã®ãweek yearã§ããuã¨Xã¯å¤§ããäºãªãã®ã§æ°ã«ãªã£ããåèªã°ã°ã£ã¦ãã ããï½
ã¡ãªã¿ã«ã1é±éã¯ææå§ã¾ãã§ã1/4ãå«ãã®ã第1é±ãã¨ããã«ã¼ã«ãç¥ã£ã¦ããã®ã¯Calendarã§ããããä¸è¨ã®éãSimpleDateFormatã«calãä¸ãã¦ãããªãã¨ãISOã®ã«ã¼ã«éãã«ã¯åãã¾ããã®ã§æ³¨æãã¾ãããã
Calendar cal = new GregorianCalendar(); cal.setMinimalDaysInFirstWeek(4); cal.setFirstDayOfWeek(Calendar.MONDAY); SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/ddã¯YYYYå¹´ã®ç¬¬wwé±ã§ãã"); f.setCalendar(cal); System.out.println(f.format(new Date()));
â¦ã¨ãã¾ãdiffããã¦ã¿ã¦æ°ã¥ããããããããã並ã¹ã¦ã¿ã¾ãããææ¥ä»¥éãå¼ãç¶ããJava Advent Calendarã§ã楽ãã¿ãã ãã :)