Mercurial > hg > Members > tatsuki > Alice
annotate src/alice/datasegment/Receiver.java @ 188:38d6a10be9c6 working
add FLIP API in Receiver class
author | e095732 |
---|---|
date | Thu, 07 Mar 2013 12:30:03 +0900 |
parents | 4658bf530834 |
children | a85ff8dc16c1 |
rev | line source |
---|---|
3 | 1 package alice.datasegment; |
2 | |
34
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
3 import java.io.IOException; |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
4 |
152 | 5 import org.msgpack.type.ArrayValue; |
18 | 6 import org.msgpack.type.Value; |
188 | 7 import org.msgpack.type.ValueFactory; |
30 | 8 import org.msgpack.type.ValueType; |
18 | 9 |
10 import alice.codesegment.InputDataSegment; | |
126 | 11 import alice.codesegment.SingletonMessage; |
18 | 12 |
57 | 13 /** |
14 * MessagePack implementation and DataSegment Receiver | |
15 * @author kazz | |
16 * | |
17 */ | |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
30
diff
changeset
|
18 public class Receiver { |
18 | 19 public InputDataSegment ids; |
20 public int index; | |
21 public Value val; | |
28
98ab26e09a98
Configuration Manager work and implements reverseKey
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
19
diff
changeset
|
22 public String from; |
18 | 23 public CommandType type; |
24 | |
57 | 25 public String managerKey; // for debugging |
26 public String key; // for debugging | |
44 | 27 |
33
20c67f673224
change name of DataSegmentReceiver
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
30
diff
changeset
|
28 public Receiver(InputDataSegment ids, CommandType type) { |
18 | 29 this.ids = ids; |
30 this.type = type; | |
19 | 31 ids.regist(); |
18 | 32 } |
33 | |
188 | 34 // for same key |
35 | |
36 public void flip(Value val){ | |
37 DataSegment.getLocal().flip(this.key, val); | |
38 } | |
39 | |
40 public void flip(int val){ | |
41 DataSegment.getLocal().flip(this.key, ValueFactory.createIntegerValue(val)); | |
42 } | |
43 | |
44 public void flip(String val){ | |
45 DataSegment.getLocal().flip(this.key, ValueFactory.createRawValue(val)); | |
46 } | |
47 | |
48 public void flip(byte[] val){ | |
49 DataSegment.getLocal().flip(this.key, ValueFactory.createRawValue(val, true)); | |
50 } | |
51 | |
52 public <T> void flip(T val) { | |
53 try { | |
54 DataSegment.getLocal().flip(this.key, SingletonMessage.getInstance().unconvert(val)); | |
55 } catch (IOException e) { | |
56 e.printStackTrace(); | |
57 } | |
58 } | |
59 | |
18 | 60 public void setKey(String managerKey, String key) { |
57 | 61 this.managerKey = managerKey; |
18 | 62 setKey(managerKey, key, 0); |
63 } | |
3 | 64 |
18 | 65 public void setKey(String managerKey, String key, int index) { |
176 | 66 this.key = key; |
18 | 67 switch (type) { |
68 case PEEK: | |
69 ids.peek(this, managerKey, key, index); | |
70 break; | |
71 case TAKE: | |
72 ids.take(this, managerKey, key, index); | |
73 break; | |
100
b01fb5090e28
add default code to each case statements
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
74 default: |
b01fb5090e28
add default code to each case statements
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
75 break; |
18 | 76 } |
19 | 77 ids.setKey(); |
18 | 78 } |
79 | |
65 | 80 public void setKey(String key) { |
81 setKey(key, 0); | |
82 } | |
83 | |
84 public void setKey(String key, int index) { | |
176 | 85 this.key = key; |
65 | 86 switch (type) { |
87 case PEEK: | |
88 ids.peek(this, key, index); | |
89 break; | |
90 case TAKE: | |
91 ids.take(this, key, index); | |
92 break; | |
100
b01fb5090e28
add default code to each case statements
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
93 default: |
b01fb5090e28
add default code to each case statements
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
88
diff
changeset
|
94 break; |
65 | 95 } |
96 ids.setKey(); | |
97 } | |
98 | |
30 | 99 public String asString() { |
100 if (val.getType() == ValueType.RAW) { | |
101 return val.asRawValue().getString(); | |
102 } | |
103 return null; | |
104 } | |
105 | |
106 public int asInteger() { | |
107 if (val.getType() == ValueType.INTEGER) { | |
108 return val.asIntegerValue().getInt(); | |
109 } | |
110 return 0; | |
111 } | |
112 | |
88 | 113 public Float asFloat() { |
114 if (val.getType() == ValueType.FLOAT) { | |
115 return val.asFloatValue().getFloat(); | |
116 } | |
117 return 0.0f; | |
118 } | |
119 | |
152 | 120 public ArrayValue asArray(){ |
121 if (val.getType() == ValueType.ARRAY){ | |
122 return val.asArrayValue(); | |
123 } | |
124 return null; | |
125 } | |
126 | |
34
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
127 public <T> T asClass(Class<T> clazz) { |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
128 try { |
126 | 129 return SingletonMessage.getInstance().convert(val, clazz); |
34
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
130 } catch (IOException e) { |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
131 e.printStackTrace(); |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
132 } |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
133 return null; |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
134 } |
ca079a730d0b
added method to OutputDataSegment and Receiver, to convert type from Value to Class<?> without MessagePack
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
33
diff
changeset
|
135 |
3 | 136 } |