/** 
 * 
 * Copyright 2004 Protique Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 * 
 **/
package org.codehaus.activemq.service;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
 * Represents a durable subscribers subscription entry which contains
 * details of the subscription and the subscriber's unique ID
 *
 * @version $Revision: 1.1 $
 */
public class SubscriberEntry implements Externalizable {
    private static final long serialVersionUID = -5754338187296859149L;

    private int subscriberID;
    private String clientID;
    private String consumerName;
    private String destination;
    private String selector;

    public SubscriberEntry() {
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        subscriberID = in.readInt();
        clientID = in.readUTF();
        consumerName = in.readUTF();
        destination = in.readUTF();
        selector = readNullableUTF(in);
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeInt(subscriberID);
        out.writeUTF(clientID);
        out.writeUTF(consumerName);
        out.writeUTF(destination);
        writeNullableUTF(out,selector);
    }

    static public String readNullableUTF(ObjectInput in) throws IOException, ClassNotFoundException {
        if( in.readBoolean() ) {
            return in.readUTF();
        } else {
            return null;
        }
    }
    
    static public void writeNullableUTF(ObjectOutput out, String value) throws IOException {
        if( value==null ) {
            out.writeBoolean(false);
        } else {
            out.writeBoolean(true);
            out.writeUTF(value);
        }
    }
    
    public String toString() {
        return super.toString() + "[clientID: " + clientID + " consumerName: " + consumerName
                + " destination: " + destination + " selector: " + selector + "]";
    }

    // Properties
    //-------------------------------------------------------------------------
    public String getClientID() {
        return clientID;
    }

    public void setClientID(String clientID) {
        this.clientID = clientID;
    }

    public String getConsumerName() {
        return consumerName;
    }

    public void setConsumerName(String consumerName) {
        this.consumerName = consumerName;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getSelector() {
        return selector;
    }

    public void setSelector(String selector) {
        this.selector = selector;
    }

    public int getSubscriberID() {
        return subscriberID;
    }

    public void setSubscriberID(int subscriberID) {
        this.subscriberID = subscriberID;
    }
}
