| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.openstack.nova.domain; |
| 20 | |
| 21 | import com.google.common.base.Function; |
| 22 | import com.google.gson.annotations.SerializedName; |
| 23 | |
| 24 | import javax.annotation.Nullable; |
| 25 | |
| 26 | /** |
| 27 | * @author Dmitri Babaev |
| 28 | */ |
| 29 | public class Address { |
| 30 | @SerializedName("addr") |
| 31 | private String address; |
| 32 | private int version; |
| 33 | |
| 34 | //for de-serialization |
| 35 | @SuppressWarnings("unused") |
| 36 | private Address() { |
| 37 | } |
| 38 | |
| 39 | public Address(String address, int version) { |
| 40 | this.address = address; |
| 41 | this.version = version; |
| 42 | } |
| 43 | |
| 44 | public String getAddress() { |
| 45 | return address; |
| 46 | } |
| 47 | |
| 48 | public int getVersion() { |
| 49 | return version; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public String toString() { |
| 54 | return address; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public boolean equals(Object o) { |
| 59 | if (this == o) return true; |
| 60 | if (o == null || getClass() != o.getClass()) return false; |
| 61 | |
| 62 | Address address1 = (Address) o; |
| 63 | |
| 64 | if (version != address1.version) return false; |
| 65 | if (address != null ? !address.equals(address1.address) : address1.address != null) return false; |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public int hashCode() { |
| 72 | int result = address != null ? address.hashCode() : 0; |
| 73 | result = 31 * result + version; |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | public static Function<Address, String> newAddress2StringFunction() { |
| 78 | return new Function<Address, String>() { |
| 79 | @Override |
| 80 | public String apply(@Nullable Address input) { |
| 81 | return input.getAddress(); |
| 82 | } |
| 83 | }; |
| 84 | } |
| 85 | |
| 86 | public static Address valueOf(String address) { |
| 87 | return new Address(address, address.startsWith("::") ? 6 : 4); |
| 88 | } |
| 89 | |
| 90 | public static Function<String, Address> newString2AddressFunction() { |
| 91 | return new Function<String, Address>() { |
| 92 | @Override |
| 93 | public Address apply(@Nullable String input) { |
| 94 | return valueOf(input); |
| 95 | } |
| 96 | }; |
| 97 | } |
| 98 | } |