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