Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 17 | package org.onosproject.store.service; |
| 18 | |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 19 | import org.onlab.util.KryoNamespace; |
| 20 | |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 21 | /** |
| 22 | * Interface for serialization for store artifacts. |
| 23 | */ |
| 24 | public interface Serializer { |
| 25 | /** |
| 26 | * Serialize the specified object. |
| 27 | * @param object object to serialize. |
| 28 | * @return serialized bytes. |
| 29 | * @param <T> encoded type |
| 30 | */ |
| 31 | <T> byte[] encode(T object); |
| 32 | |
| 33 | /** |
| 34 | * Deserialize the specified bytes. |
| 35 | * @param bytes byte array to deserialize. |
| 36 | * @return deserialized object. |
| 37 | * @param <T> decoded type |
| 38 | */ |
| 39 | <T> T decode(byte[] bytes); |
Madan Jampani | b5d72d5 | 2015-04-03 16:53:50 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Creates a new Serializer instance from a KryoNamespace. |
| 43 | * |
| 44 | * @param kryo kryo namespace |
| 45 | * @return Serializer instance |
| 46 | */ |
| 47 | public static Serializer using(KryoNamespace kryo) { |
| 48 | return new Serializer() { |
| 49 | @Override |
| 50 | public <T> byte[] encode(T object) { |
| 51 | return kryo.serialize(object); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public <T> T decode(byte[] bytes) { |
| 56 | return kryo.deserialize(bytes); |
| 57 | } |
| 58 | }; |
| 59 | } |
Madan Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 60 | } |