IPAddressValue.java
/*
* Copyright 2007-2008 Yoav Abrahami
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.abra.j2xb.exampleBeans.customType;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Yoav Abrahami
* @version 1.0, May 1, 2008
* @since JDK1.5
*/
public class IPAddressValue {
private final static String IPAddressPattern = "([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\." +
"([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\." +
"([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\." +
"([0-9]|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])";
private byte[] parts = new byte[4];
public IPAddressValue() {
}
public IPAddressValue(String value) {
Matcher matcher = Pattern.compile(IPAddressPattern).matcher(value);
if (matcher.find()) {
parts[0] = Byte.parseByte(matcher.group(1));
parts[1] = Byte.parseByte(matcher.group(2));
parts[2] = Byte.parseByte(matcher.group(3));
parts[3] = Byte.parseByte(matcher.group(4));
}
}
public byte[] getParts() {
return parts;
}
public void setParts(byte[] parts) {
this.parts = parts;
}
public static boolean isValidStringValue(String value) {
return value != null &&
value.matches(IPAddressPattern);
}
public String toString() {
return String.format("%d.%d.%d.%d", parts[0], parts[1], parts[2], parts[3]);
}
}