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

gu.dtalk.IPv4Option Maven / Gradle / Ivy

There is a newer version: 0.8.2
Show newest version
package gu.dtalk;

import java.net.InetAddress;

import com.gitee.l0km.com4j.basex.NetworkUtil;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;

/**
 * IP(ipv4)地址选项类型
 * @author guyadong
 *
 */
public class IPv4Option extends BaseBinary {
	public static final Predicate VALIDATOR = new Predicate() {
		@Override
		public boolean apply(byte[] input) {
			return input != null && input.length==4;
		}
	};
	public IPv4Option() {
		setValidator(VALIDATOR);
	}
	@Override
	public OptionType getType() {
		return OptionType.IP;
	}
	@Override
	public String contentOfValue() {
		if(getValue() == null){
			return super.contentOfValue();
		}
		return NetworkUtil.formatIp((byte[]) getValue());		
	}
	public IPv4Option asValue(InetAddress input) {
		try {
			setValue(input.getAddress());
			return this;
		} catch (Throwable e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
	public IPv4Option asDefaultValue(InetAddress input) {
		try {
			setDefaultValue(input.getAddress());
			return this;
		} catch (Throwable e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy