from old Java to modern Java

  • View
    31

  • Download
    2

Embed Size (px)

DESCRIPTION

JJUGナイトセミナー2013年6月の発表資料です

Text of from old Java to modern Java

  • from old Javato modern Java JavaAcroquest TechnologyJJUG / Java ( @cero_t )

Javagihyo.jp ENdoSnipeOSSJavagihyo.jpendosnipe gihyohttp://www.endosnipe.com 20137JavaJava4http://www.amazon.co.jp Java JavaAPI Java JavaSE7 OracleJavaSE6 J2SE 5.0 J2SE 1.4 1.45.0 JavaSE8 from old Javato modern Java JavaAcroquest TechnologyJJUG / Java ( @cero_t ) Lesson1 private List m_list = null;private int process_le(String str_le_name) {String str_line;List list_lines = new ArrayList();int i_result = read_le(str_le_name, list_lines);if (i_result == 0) {List list_record = new ArrayList();for (int i = 0; i < list_lines.size(); i++) {str_line = (String) list_lines.get(i);Record record = new Record();i_result = parse_line(str_line, record);if (i_result != 0) {return i_result;}list_recordord.add(record);}m_list = list_record;return 0;} else {return i_result;}} Lesson1from C-ish Javato Java like Java(J2SE1.4) private List m_list = null;private int process_le(String str_le_name) {String str_line;List list_lines = new ArrayList();int i_result = read_le(str_le_name, list_lines);if (i_result == 0) {List list_record = new ArrayList();... private List m_list = null;private int process_le(String str_le_name) {String str_line;List list_lines = new ArrayList();int i_result = read_le(str_le_name, list_lines);if (i_result == 0) {List list_record = new ArrayList();...m_ 0 for (int i = 0; i < list_lines.size(); i++) {str_line = (String) list_lines.get(i);Record record = new Record();i_result = parse_line(str_line, record);if (i_result != 0) {return i_result;}list_recordord.add(record);}m_list = list_record;return 0; for (int i = 0; i < list_lines.size(); i++) {str_line = (String) list_lines.get(i);Record record = new Record();i_result = parse_line(str_line, record);if (i_result != 0) {return i_result;}list_recordord.add(record);}m_list = list_record;return 0;return 0; Java private List resultList;private List processFile(String leName)throws SystemException {List lines = readFile(leName);List recordList = new ArrayList();for (int i = 0; i < lines.size(); i++) {String line = (String) lines.get(i);Record record = parseLine(line);recordList.add(record);}return recordList;} private List resultList;private List processFile(String leName)throws SystemException {List lines = readFile(leName);List recordList = new ArrayList();for (int i = 0; i < lines.size(); i++) {String line = (String) lines.get(i);Record record = parseLine(line);recordList.add(record);}return recordList;}thisExceptionNo before / after before private List m_list = null;private int process_le(String str_le_name) {String str_line;List list_lines = new ArrayList();int i_result = read_le(str_le_name, list_lines);if (i_result == 0) {List list_record = new ArrayList();for (int i = 0; i < list_lines.size(); i++) {str_line = (String) list_lines.get(i);Record record = new Record();i_result = parse_line(str_line, record);if (i_result != 0) {return i_result;}list_recordord.add(record);}m_list = list_record;return 0;} else {return i_result;}} after private List resultList;private List processFile(String leName)throws SystemException {List lines = readFile(leName);List recordList = new ArrayList();for (int i = 0; i < lines.size(); i++) {String line = (String) lines.get(i);Record record = parseLine(line);recordList.add(record);}return recordList;} No C-ish Java like1IDEthis2 340Exception5 Lesson1 Lesson2from J2SE 1.4to J2SE 5.0 JavaSE 5.0J2SE 5.0 J2SE 5.0 Generics foreachenumstatic importvar argsauto boxing... and more Java before private List processFile(String leName)throws SystemException {List lines = readFile(leName);List recordList = new ArrayList();for (int i = 0; i < lines.size(); i++) {String line = (String) lines.get(i);Record record = parseLine(line);recordList.add(record);}return recordList;} after private List processFile(String leName)throws SystemException {List lines = readFile(leName);List recordList = new ArrayList();for (String line : lines) {Record record = parseLine(line);recordList.add(record);}return recordList;} private List processFile(String leName)throws SystemException {List lines = readFile(leName);List recordList = new ArrayList();for (String line : lines) {Record record = parseLine(line);recordList.add(record);}return recordList;}Genericsforeachfor public interface Constants {public static nal int FILE_NOT_FOUND = -1;public static nal int FILE_READ_ERROR = -2;public static nal int FILE_EMPTY = -3;public static nal int RECORD_EMPTY = -4;public static nal int RECORD_SIZE_ERROR = -5;public static nal int RECORD_BODY_EMPTY = -6;} public class FileProcessor implements Constants {private List readFile(String leName) {List lines = new ArrayList();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(leName));String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);} nally {try {if (reader != null) {reader.close();}} catch (IOException ex) {// }}return lines;}} public class FileProcessor implements Constants {private List readFile(String leName) {List lines = new ArrayList();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(leName));String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);} nally {try {if (reader != null) {reader.close();}} catch (IOException ex) {// }}return lines;}} public enum ErrorCode {FILE_NOT_FOUND, FILE_READ_ERROR, FILE_EMPTY, RECORD_EMPTY, RECORD_SIZE_ERROR, RECORD_BODY_EMPTY, RECODE_CODE_NOT_NUMERIC;}enum import static ErrorCode.*;public class FileProcessor {private List readFile(String leName) {List lines = new ArrayList();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(leName));String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);} nally {try {if (reader != null) {reader.close();}} catch (IOException ex) {// }}return lines;}}static importenumimport import static1 * IDE No J2SE 1.4 J2SE 5.01 ObjectCollection GenericsCollection2for (int i=0; i < list.size(); i++)while (iterator.hasNext())for (String value : list)3 static import4 int enumLesson2 Lesson3from J2SE 5.0to JavaSE 6 JavaSE 6API APInative2ascii native2asciiIDE No J2SE 5.0 JavaSE 61native2asciiIDEnative2asciiIDELesson3 Lesson4from JavaSE 6to JavaSE 7 JavaSE 7 try-with-resourcesdiamond operatornio2multi-catch... and more private List readFile(String leName) {List lines = new ArrayList();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(leName));String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);} nally {try {if (reader != null) {reader.close();}} catch (IOException ex) {// }}return lines;} private List readFile(String leName) {List lines = new ArrayList();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(leName));String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);} nally {try {if (reader != null) {reader.close();}} catch (IOException ex) {// }}return lines;}nallyclose nallyclose private List readFile(String leName) {List lines = new ArrayList();try (FileReader in = new FileReader(leName);BufferedReader reader= new BufferedReader(in)) {String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);}return lines;} private List readFile(String leName) {List lines = new ArrayList();try (FileReader in = new FileReader(leName);BufferedReader reader= new BufferedReader(in)) {String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);}return lines;}trytrydiamond private List readFile(String leName) {try {return Files.readAllLines(Paths.get(leName),Charset.defaultCharset());} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);}} private List readFile(String leName) {try {return Files.readAllLines(Paths.get(leName),Charset.defaultCharset());} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);}}NIO2API Groovy before private List readFile(String leName) {List lines = new ArrayList();BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(leName));String line;while ((line = reader.readLine()) != null) {lines.add(line);}} catch (FileNotFoundException ex) {throw new SystemException(FILE_NOT_FOUND, ex);} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);} nally {try {if (reader != null) {reader.close();}} catch (IOException ex) {// }}return lines;} after private List readFile(String leName) {try {return Files.readAllLines(Paths.get(leName),Charset.defaultCharset());} catch (IOException ex) {throw new SystemException(FILE_READ_ERROR, ex);}} API before public class Entity {private int intValue;private long longValue;private oat oatValue;private String string;private Date date;@Overridepublic int hashCode() {int result = intValue;result = 31 * result + (int) (longValue ^ (longVal