This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/* | |
* Stone.java | |
* | |
* class to support immutable collection handling | |
* | |
* @author kei sugano | |
* | |
* | |
*/ | |
public class Stone { | |
public static <T> List<T> newList(T e){ | |
List<T> list = new ArrayList<T>(){{ add(e); }}; | |
return list; | |
} | |
public static <T> List<T> newList(T e, T e2){ | |
return push(newList(e), e2); | |
} | |
public static <T> List<T> newList(T e, T e2, T e3){ | |
return push(newList(e, e2), e3); | |
} | |
public static <T> List<T> newList(T e, T e2, T e3, T e4){ | |
return push(newList(e, e2, e3), e4); | |
} | |
public static <T> List<T> newList(T e, T e2, T e3, T e4, T e5){ | |
return push(newList(e, e2, e3, e4), e5); | |
} | |
public static <T> List<T> newList(T e, T e2, T e3, T e4, T e5, T e6){ | |
return push(newList(e, e2, e3, e4, e5), e6); | |
} | |
public static <T> List<T> concatList(List<T> list1, List<T> list2){ | |
List<T> _list = new ArrayList<T>(); | |
_list.addAll(list1); | |
_list.addAll(list2); | |
return _list; | |
} | |
public static <T> List<T> toList(T[] args){ | |
return Arrays.asList(args); | |
} | |
public static <T> List<T> push(List<T> list, T elem){ | |
List<T> _list = new ArrayList<T>(); | |
for(T t : list) | |
_list.add(t); | |
_list.add(elem); | |
return _list; | |
} | |
public static <T> T get(List<T> list, Integer i){ | |
return list.get(i); | |
} | |
public static <T> void puts(T text){ | |
System.out.println(text); | |
} | |
public static <T> void puts(List<T> list){ | |
System.out.print("["); | |
for(T t : list) | |
System.out.print(t.toString() + ","); | |
System.out.print("]"); | |
System.out.println(""); | |
} | |
} |
俺天才過ぎ(確信)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main{ | |
public static void main(String args[]){ | |
Stone.puts(Stone.get(Stone.push(Stone.toList(args), "aaa"), 0)); | |
immutableListTest(args); | |
concatListTest(); | |
} | |
private static <T> void immutableListTest(String args[]){ | |
List<String> immutableList = Stone.toList(args); | |
immutableList = Stone.push(immutableList, "hahaha"); | |
for(String t: immutableList) | |
Stone.puts(t); | |
} | |
public static void concatListTest(){ | |
List<Integer> _list1 = new ArrayList<Integer>(){{add(1); }}; | |
Stone.puts(Stone.concatList(Stone.newList(1, 2), Stone.newList(3))); | |
Stone.puts(Stone.concatList(Stone.newList(4,5,6), Stone.newList(7))); | |
Stone.puts(Stone.concatList(Stone.newList("a","b","c","d","e", "f"), Stone.newList("i"))); | |
} | |
} |
呼び出し側 https://github.com/keitaroemotion/Stone
No comments:
Post a Comment