All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sdnwiselab.sdnwise.util.NodeAddress Maven / Gradle / Ivy

/* 
 * Copyright (C) 2015 SDN-WISE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.github.sdnwiselab.sdnwise.util;

import java.io.Serializable;

/**
 *
 * @author Sebastiano Milardo
 * @version %I%, %G%
 */
public final class NodeAddress implements Comparable, Serializable {

    private static final long serialVersionUID = 1L;

    private final byte[] addr = new byte[2];

    /**
     *
     * @param addr
     */
    public NodeAddress(int addr) {
        this.addr[0] = (byte) (addr >> 8);
        this.addr[1] = (byte) (addr & 0xFF);
    }

    /**
     *
     * @param addr
     */
    public NodeAddress(byte[] addr) {
        if (addr.length == 2) {
            this.addr[0] = addr[0];
            this.addr[1] = addr[1];
        }
    }

    /**
     *
     * @param addr
     */
    public NodeAddress(String addr) {
        String[] add = addr.split("\\s*\\.\\s*");
        this.addr[0] = (byte) Integer.parseInt(add[0]);
        this.addr[1] = (byte) Integer.parseInt(add[1]);
    }

    /**
     *
     * @param addr0
     * @param addr1
     */
    public NodeAddress(int addr0, int addr1) {
        this.addr[0] = (byte) addr0;
        this.addr[1] = (byte) addr1;
    }

    /**
     *
     * @return
     */
    public int intValue() {
        return ((addr[0] & 0xFF) * 256) + (addr[1] & 0xFF);
    }

    /**
     *
     * @return
     */
    public byte getHigh() {
        return addr[0];
    }

    /**
     *
     * @return
     */
    public byte getLow() {
        return addr[1];
    }

    public Byte[] getArray() {
        return new Byte[]{addr[0], addr[1]};
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return ((addr[0] & 0xFF) + "." + (addr[1] & 0xFF));
    }

    /**
     *
     * @param other
     * @return
     */
    @Override
    public int compareTo(NodeAddress other) {
        return Integer.valueOf(this.intValue()).compareTo(other.intValue());
    }

    /**
     *
     * @return
     */
    @Override
    public int hashCode() {
        return Integer.valueOf(this.intValue()).hashCode();
    }

    /**
     *
     * @param obj
     * @return
     */
    @Override
    public boolean equals(Object obj) {
        return obj instanceof NodeAddress && ((NodeAddress) obj).intValue() == this.intValue();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy