001package gu.dtalk;
002
003import java.net.NetworkInterface;
004
005import com.google.common.base.Predicate;
006import com.google.common.base.Throwables;
007
008import net.gdface.utils.NetworkUtil;
009
010/**
011 * mac(6 bytes)地址选项类型
012 * @author guyadong
013 *
014 */
015public class MACOption 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 == 6;
020                }
021        };
022        public MACOption() {
023                setValidator(VALIDATOR);
024        }
025        @Override
026        public OptionType getType() {
027                return OptionType.MAC;
028        }
029
030        @Override
031        public String contentOfValue() {
032                if(getValue() == null){
033                        return super.contentOfValue();
034                }
035                return NetworkUtil.formatMac((byte[]) getValue(), ":");         
036        }
037        public MACOption asValue(NetworkInterface input) {
038                try {
039                        setValue(input.getHardwareAddress());
040                        return this;
041                } catch (Exception e) {
042                        Throwables.throwIfUnchecked(e);
043                        throw new RuntimeException(e);
044                }
045        }
046        public MACOption asDefaultValue(NetworkInterface input) {
047                try {
048                        setDefaultValue(input.getHardwareAddress());
049                        return this;
050                } catch (Exception e) {
051                        Throwables.throwIfUnchecked(e);
052                        throw new RuntimeException(e);
053                }
054        }
055}