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.naming.serialization; 020 021import org.apache.reef.io.network.naming.avro.AvroNamingUnRegisterRequest; 022import org.apache.reef.wake.IdentifierFactory; 023import org.apache.reef.wake.remote.Codec; 024 025import javax.inject.Inject; 026 027/** 028 * Naming un-registration request codec. 029 */ 030public final class NamingUnregisterRequestCodec implements Codec<NamingUnregisterRequest> { 031 032 private final IdentifierFactory factory; 033 034 /** 035 * Constructs a naming un-registration request codec. 036 * 037 * @param factory the identifier factory 038 */ 039 @Inject 040 public NamingUnregisterRequestCodec(final IdentifierFactory factory) { 041 this.factory = factory; 042 } 043 044 /** 045 * Encodes the naming un-registration request to bytes. 046 * 047 * @param obj the naming un-registration request 048 * @return a byte array 049 */ 050 @Override 051 public byte[] encode(final NamingUnregisterRequest obj) { 052 final AvroNamingUnRegisterRequest result = AvroNamingUnRegisterRequest.newBuilder() 053 .setId(obj.getIdentifier().toString()) 054 .build(); 055 return AvroUtils.toBytes(result, AvroNamingUnRegisterRequest.class); 056 } 057 058 /** 059 * Decodes the bytes to a naming un-registration request. 060 * 061 * @param buf the byte array 062 * @return a naming un-registration request 063 * @throws org.apache.reef.io.network.naming.exception.NamingRuntimeException 064 */ 065 @Override 066 public NamingUnregisterRequest decode(final byte[] buf) { 067 final AvroNamingUnRegisterRequest result = AvroUtils.fromBytes(buf, AvroNamingUnRegisterRequest.class); 068 return new NamingUnregisterRequest(factory.getNewInstance(result.getId().toString())); 069 } 070 071}