001package gu.dtalk;
002
003import java.net.InetAddress;
004
005import com.google.common.base.Predicate;
006import com.google.common.base.Throwables;
007
008import net.gdface.utils.NetworkUtil;
009
010/**
011 * IP(ipv4)地址选项类型
012 * @author guyadong
013 *
014 */
015public class IPv4Option extends BaseBinary {
016        public static final Predicate<byte[]> VALIDATOR = new Predicate<byte[]>() {
017                @Override
018                public boolean apply(byte[] input) {
019                        return input != null && input.length==4;
020                }
021        };
022        public IPv4Option() {
023                setValidator(VALIDATOR);
024        }
025        @Override
026        public OptionType getType() {
027                return OptionType.IP;
028        }
029        @Override
030        public String contentOfValue() {
031                if(getValue() == null){
032                        return super.contentOfValue();
033                }
034                return NetworkUtil.formatIp((byte[]) getValue());               
035        }
036        public IPv4Option asValue(InetAddress input) {
037                try {
038                        setValue(input.getAddress());
039                        return this;
040                } catch (Exception e) {
041                        Throwables.throwIfUnchecked(e);
042                        throw new RuntimeException(e);
043                }
044        }
045        public IPv4Option asDefaultValue(InetAddress input) {
046                try {
047                        setDefaultValue(input.getAddress());
048                        return this;
049                } catch (Exception e) {
050                        Throwables.throwIfUnchecked(e);
051                        throw new RuntimeException(e);
052                }
053        }
054}