1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.writable.kryo.serializers;
1920import java.util.Collections;
21import java.util.List;
2223import com.esotericsoftware.kryo.Kryo;
24import com.esotericsoftware.kryo.Serializer;
25import com.esotericsoftware.kryo.io.Input;
26import com.esotericsoftware.kryo.io.Output;
2728/**29 * Special serializer for Collections.nCopies30 *31 * @param <T> Element type32 */33publicclass CollectionsNCopiesSerializer<T> extends Serializer<List<T>> {
34 @Override
35publicvoid write(Kryo kryo, Output output, List<T> object) {
36 output.writeInt(object.size(), true);
37if (object.size() > 0) {
38 kryo.writeClassAndObject(output, object.get(0));
39 }
40 }
4142 @Override
43public List<T> read(Kryo kryo, Input input, Class<List<T>> type) {
44int size = input.readInt(true);
45if (size > 0) {
46 T object = (T) kryo.readClassAndObject(input);
47return Collections.nCopies(size, object);
48 } else {
49return Collections.emptyList();
50 }
51 }
52 }