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 *
019 */
020package org.apache.mina.transport.vmpipe;
021
022import java.net.SocketAddress;
023
024/**
025 * A {@link SocketAddress} which represents in-VM pipe port number.
026 *
027 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
028 */
029public class VmPipeAddress extends SocketAddress implements Comparable<VmPipeAddress> {
030    private static final long serialVersionUID = 3257844376976830515L;
031
032    private final int port;
033
034    /**
035     * Creates a new instance with the specifid port number.
036     * 
037     * @param port the port to use
038     */
039    public VmPipeAddress(int port) {
040        this.port = port;
041    }
042
043    /**
044     * @return the port number.
045     */
046    public int getPort() {
047        return port;
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public int hashCode() {
055        return port;
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public boolean equals(Object o) {
063        if (this == o) {
064            return true;
065        }
066
067        if (o instanceof VmPipeAddress) {
068            VmPipeAddress that = (VmPipeAddress) o;
069            return this.port == that.port;
070        }
071
072        return false;
073    }
074
075    /**
076     * {@inheritDoc}
077     */
078    public int compareTo(VmPipeAddress o) {
079        return this.port - o.port;
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public String toString() {
087        if (port >= 0) {
088            return "vm:server:" + port;
089        }
090
091        return "vm:client:" + -port;
092    }
093}