Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf900224

Browse files
committed
Consumer: add product model
1 parentfccd29c commitf900224

File tree

1 file changed

+195
-0
lines changed
  • Consumer/src/main/java/com/telkomdev/consumer/model

1 file changed

+195
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
* Copyright 2019 wuriyanto.com
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
packagecom.telkomdev.consumer.model;
18+
19+
importcom.google.protobuf.InvalidProtocolBufferException;
20+
importcom.telkomdev.consumer.avrojava.ProductAvro;
21+
importcom.telkomdev.consumer.protojava.ProductProto;
22+
importcom.telkomdev.consumer.deserializer.JsonParser;
23+
importorg.apache.avro.io.*;
24+
importorg.apache.avro.specific.SpecificDatumReader;
25+
importorg.apache.avro.specific.SpecificDatumWriter;
26+
27+
importjava.io.ByteArrayOutputStream;
28+
importjava.io.IOException;
29+
importjava.io.InputStream;
30+
importjava.util.ArrayList;
31+
importjava.util.List;
32+
33+
publicclassProduct {
34+
35+
privateStringid;
36+
privateStringname;
37+
privateIntegerquantity;
38+
privateList<String>images;
39+
40+
publicProduct() {
41+
42+
}
43+
44+
publicProduct(Stringid,Stringname,Integerquantity,List<String>images) {
45+
this.id =id;
46+
this.name =name;
47+
this.quantity =quantity;
48+
this.images =images;
49+
}
50+
51+
publicStringgetId() {
52+
returnid;
53+
}
54+
55+
publicvoidsetId(Stringid) {
56+
this.id =id;
57+
}
58+
59+
publicStringgetName() {
60+
returnname;
61+
}
62+
63+
publicvoidsetName(Stringname) {
64+
this.name =name;
65+
}
66+
67+
publicIntegergetQuantity() {
68+
returnquantity;
69+
}
70+
71+
publicvoidsetQuantity(Integerquantity) {
72+
this.quantity =quantity;
73+
}
74+
75+
publicList<String>getImages() {
76+
returnimages;
77+
}
78+
79+
publicvoidsetImages(List<String>images) {
80+
this.images =images;
81+
}
82+
83+
@Override
84+
publicStringtoString() {
85+
return"Product{" +
86+
"id='" +id +'\'' +
87+
", name='" +name +'\'' +
88+
", quantity=" +quantity +
89+
", images=" +images +
90+
'}';
91+
}
92+
93+
publicbyte[]toAvro()throwsIOException {
94+
DatumWriter<ProductAvro>datumWriter =newSpecificDatumWriter<>(ProductAvro.class);
95+
ProductAvroproductAvro =newProductAvro();
96+
productAvro.setId(this.id);
97+
productAvro.setName(this.name);
98+
productAvro.setQuantity(this.quantity);
99+
100+
List<CharSequence>imagesAvro =newArrayList<>();
101+
for (Stringimage :this.images) {
102+
CharSequenceim =newStringBuilder(image);
103+
imagesAvro.add(im);
104+
}
105+
productAvro.setImages(imagesAvro);
106+
107+
ByteArrayOutputStreamoutputStream =newByteArrayOutputStream();
108+
Encoderencoder =EncoderFactory.get().binaryEncoder(outputStream,null);
109+
try {
110+
datumWriter.write(productAvro,encoder);
111+
encoder.flush();
112+
outputStream.close();
113+
}catch (IOExceptionex) {
114+
throwex;
115+
}
116+
117+
returnoutputStream.toByteArray();
118+
}
119+
120+
publicstaticProductfromAvro(byte[]in)throwsIOException {
121+
ProductAvroproductAvro =null;
122+
Productproduct =newProduct();
123+
DatumReader<ProductAvro>datumReader =newSpecificDatumReader<>(ProductAvro.class);
124+
Decoderdecoder =DecoderFactory.get().binaryDecoder(in,null);
125+
126+
try {
127+
productAvro =datumReader.read(null,decoder);
128+
}catch (IOExceptionex) {
129+
throwex;
130+
}
131+
132+
product.setId(productAvro.getId().toString());
133+
product.setName(productAvro.getName().toString());
134+
product.setQuantity(productAvro.getQuantity());
135+
136+
List<String>images =newArrayList<>();
137+
for (CharSequencesb :productAvro.getImages()) {
138+
images.add(sb.toString());
139+
}
140+
141+
product.setImages(images);
142+
143+
returnproduct;
144+
}
145+
146+
publicbyte[]toJson() {
147+
JsonParser<Product>jp =newJsonParser<>(this,Product.class);
148+
returnjp.serialize();
149+
}
150+
151+
publicstaticProductfromJson(byte[]in) {
152+
JsonParser<Product>jp =newJsonParser<>(Product.class);
153+
returnjp.deserialize(in);
154+
}
155+
156+
publicProductProto.ProducttoProto() {
157+
ProductProto.ProductproductOut =ProductProto.Product.newBuilder()
158+
.setID(this.id)
159+
.setName(this.name)
160+
.setQuantity(this.quantity)
161+
.addAllImages(this.images)
162+
.build();
163+
returnproductOut;
164+
}
165+
166+
publicstaticProductfromProto(byte[]in)throwsInvalidProtocolBufferException {
167+
ProductProto.ProductproductIn =ProductProto.Product.parseFrom(in);
168+
Productproduct =newProduct();
169+
product.setId(productIn.getID());
170+
product.setName(productIn.getName());
171+
product.setQuantity((int)productIn.getQuantity());
172+
List<String>images =newArrayList<>();
173+
for (Stringimage :productIn.getImagesList()) {
174+
images.add(image);
175+
}
176+
177+
product.setImages(images);
178+
returnproduct;
179+
}
180+
181+
publicstaticProductfromProto(InputStreamin)throwsIOException {
182+
ProductProto.ProductproductIn =ProductProto.Product.parseFrom(in);
183+
Productproduct =newProduct();
184+
product.setId(productIn.getID());
185+
product.setName(productIn.getName());
186+
product.setQuantity((int)productIn.getQuantity());
187+
List<String>images =newArrayList<>();
188+
for (Stringimage :productIn.getImagesList()) {
189+
images.add(image);
190+
}
191+
192+
product.setImages(images);
193+
returnproduct;
194+
}
195+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp