Mercurial > hg > Members > nobuyasu > PracticeJava
changeset 3:5b77974d641b
add ListValue.java
author | one |
---|---|
date | Sat, 08 Jun 2013 20:43:52 +0900 |
parents | 79c8067c34ec |
children | a7cd9a10033b |
files | .classpath src/ie/oshiro/messagepack/jungle/practice/ListValue.java src/ie/oshiro/messagepack/practice/MSInterFaceTest.java src/ie/oshiro/messagepack/practice/MyClass.java |
diffstat | 4 files changed, 115 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/.classpath Fri Jun 07 20:26:28 2013 +0900 +++ b/.classpath Sat Jun 08 20:43:52 2013 +0900 @@ -6,5 +6,6 @@ <classpathentry kind="lib" path="/Users/nobuyasu/workspace/PracticeJava/lib/javassist-3.16.1-GA.jar"/> <classpathentry kind="lib" path="/Users/nobuyasu/workspace/PracticeJava/lib/msgpack-0.6.8-SNAPSHOT-sources.jar"/> <classpathentry kind="lib" path="/Users/nobuyasu/workspace/PracticeJava/lib/msgpack-0.6.8-SNAPSHOT.jar"/> + <classpathentry kind="lib" path="/Users/nobuyasu/workspace/jungle-core/target/jungle-core-0.0.1-SNAPSHOT.jar"/> <classpathentry kind="output" path="bin"/> </classpath>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ie/oshiro/messagepack/jungle/practice/ListValue.java Sat Jun 08 20:43:52 2013 +0900 @@ -0,0 +1,51 @@ +package ie.oshiro.messagepack.jungle.practice; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.msgpack.MessagePack; +import org.msgpack.template.ListTemplate; +import org.msgpack.template.ValueTemplate; +import org.msgpack.type.Value; + +public class ListValue { + + public Value iterValue; + + public ListValue() { + iterValue = null; + } + + public void setList(List<Integer> list) throws IOException { + MessagePack msgpack = new MessagePack(); + Value v = msgpack.unconvert(list); + iterValue = v; + } + + public List<Value> getList() throws IOException { + MessagePack msgpack = new MessagePack(); + msgpack.register(List.class, new ListTemplate(ValueTemplate.getInstance())); + return (List<Value>)msgpack.convert(iterValue, List.class); + } + + public void setIterValue(Value iter) { + iterValue = iter; + } + + public Value getIterValue() { + return iterValue; + } + + public static void main(String[] args) throws IOException { + List<Integer> list = Arrays.asList(1,2,3,4); + ListValue lv = new ListValue(); + lv.setList(list); + + List<Value> convertedList = lv.getList(); + for (Value v: convertedList) { + System.out.println(v.asIntegerValue()); + } + } + +}
--- a/src/ie/oshiro/messagepack/practice/MSInterFaceTest.java Fri Jun 07 20:26:28 2013 +0900 +++ b/src/ie/oshiro/messagepack/practice/MSInterFaceTest.java Sat Jun 08 20:43:52 2013 +0900 @@ -14,19 +14,29 @@ @SuppressWarnings("unchecked") public static void main(String[] args) throws IOException { + /* No Error : Int Array, Class Array */ MessagePack msgpack = new MessagePack(); MyClass m = new MyClass(5); + m.setIntArray(new int[]{8,9,10}); + MyClass[] myClassArray = {new MyClass(11), new MyClass(12)}; + m.setMyClassArray(myClassArray); Value v = msgpack.unconvert(m); MyClass mm = (MyClass)msgpack.convert(v, MyClass.class); - System.out.println("num = " + mm.getNum()); - + System.out.println("mm.getNum() = " + mm.getNum()); + int[] intArray = mm.getIntArray(); + for (int i=0; i<intArray.length; i++) { + System.out.println("intArray["+i+"] = "+ intArray[i]); + } + MyClass[] myClassArray2 =mm.getMyClassArray(); + for (int i=0; i<myClassArray2.length; i++) { + System.out.println("myClassArray2 getNum() : "+ myClassArray2[i].getNum()); + } + /* No Error : List*/ LinkedList<MyClass> list = new LinkedList<MyClass>(); - list.add(new MyClass()); - list.add(new MyClass()); - list.add(new MyClass()); -// m.setIter(list); - + list.add(new MyClass(1)); + list.add(new MyClass(2)); + list.add(new MyClass(3)); MessagePack msgpack2 = new MessagePack(); byte[] bytes; bytes = msgpack2.write(list); @@ -36,5 +46,19 @@ System.out.println("m2 = "+m2.getNum()); } + /* Error: Iterable in Class */ + MessagePack msgpack3 = new MessagePack(); + MyClass m3 = new MyClass(3); + LinkedList<Value> list2 = new LinkedList<Value>(); + list2.add(v); + m3.setIter(list2); + /* convert into unknown type is invalid */ + byte[] bytes2; + bytes2 = msgpack3.write(m3); + MyClass mm3 = msgpack3.read(bytes2, MyClass.class); +// Value v3 = msgpack.unconvert(m3); +// MyClass mm3 = (MyClass)msgpack.convert(v3, MyClass.class); + System.out.println("mm3.getNum() = "+mm3.getNum()); + } }
--- a/src/ie/oshiro/messagepack/practice/MyClass.java Fri Jun 07 20:26:28 2013 +0900 +++ b/src/ie/oshiro/messagepack/practice/MyClass.java Sat Jun 08 20:43:52 2013 +0900 @@ -1,28 +1,57 @@ package ie.oshiro.messagepack.practice; import org.msgpack.annotation.Message; +import org.msgpack.type.Value; +import jp.ac.u_ryukyu.ie.cr.shoshi.jungle.store.Command; + @Message public class MyClass implements MyInterface { - private Iterable<MyClass> iter; + public Iterable<Value> iter; + private int[] intArray; + private MyClass[] myClassArray; + public Value iterValue; int num; MyClass() { iter = null; + myClassArray = null; num = 0; } MyClass(int i) { iter = null; + myClassArray = null; num = i; } public int getNum() { return num; } public void setNum(int i) { num = i; } - public void setIter(Iterable<MyClass> _iter) { + public void setIter(Iterable<Value> _iter) { + + + iter = _iter; } - public Iterable<MyClass> getIter() { + public Iterable<Value> getIter() { return iter; } + + public void setIntArray(int[] array) { + intArray = array; + } + + public int[] getIntArray() { + return intArray; + } + + public void setMyClassArray(MyClass[] array) { + myClassArray = array; + } + + public MyClass[] getMyClassArray() { + return myClassArray; + } + + }