001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.io.network.group.impl; 020 021import org.apache.reef.io.network.group.api.GroupChanges; 022import org.apache.reef.io.serialization.Codec; 023 024import javax.inject.Inject; 025import java.util.logging.Level; 026import java.util.logging.Logger; 027 028public class GroupChangesCodec implements Codec<GroupChanges> { 029 private static final Logger LOG = Logger.getLogger(GroupChangesCodec.class.getName()); 030 031 @Inject 032 public GroupChangesCodec() { 033 } 034 035 @Override 036 public GroupChanges decode(final byte[] changeBytes) { 037 return new GroupChangesImpl(changeBytes[0] == 1); 038 } 039 040 @Override 041 public byte[] encode(final GroupChanges changes) { 042 final byte[] retVal = new byte[1]; 043 if (changes.exist()) { 044 retVal[0] = 1; 045 } 046 return retVal; 047 } 048 049 public static void main(final String[] args) { 050 GroupChanges changes = new GroupChangesImpl(false); 051 final GroupChangesCodec changesCodec = new GroupChangesCodec(); 052 GroupChanges changes1 = changesCodec.decode(changesCodec.encode(changes)); 053 test(changes, changes1); 054 changes = new GroupChangesImpl(true); 055 changes1 = changesCodec.decode(changesCodec.encode(changes)); 056 test(changes, changes1); 057 } 058 059 private static void test(final GroupChanges changes, final GroupChanges changes1) { 060 final boolean c1 = changes.exist(); 061 final boolean c2 = changes1.exist(); 062 063 if (c1 != c2) { 064 LOG.log(Level.SEVERE, "Something is wrong: {0} != {1}", new Object[]{c1, c2}); 065 } else { 066 LOG.log(Level.INFO, "Codec is fine"); 067 } 068 } 069}