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