001/*
002 * Copyright 2010-2014 Ning, Inc.
003 * Copyright 2014-2015 The Billing Project, LLC
004 *
005 * The Billing Project licenses this file to you under the Apache License, version 2.0
006 * (the "License"); you may not use this file except in compliance with the
007 * License.  You may obtain a copy of the License at:
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
014 * License for the specific language governing permissions and limitations
015 * under the License.
016 */
017package com.ning.billing.recurly.model;
018
019import com.google.common.base.Objects;
020
021import javax.xml.bind.annotation.XmlElement;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027
028@XmlRootElement(name = "measured_unit")
029public class MeasuredUnit extends RecurlyObject{
030
031    @XmlTransient
032    public static final String MEASURED_UNITS_RESOURCE = "/measured_units";
033
034    @XmlTransient
035    public static final Pattern MEASURED_UNITS_CODE_PATTERN = Pattern.compile(MEASURED_UNITS_RESOURCE + "/(.+)$");
036
037    @XmlElement(name = "id")
038    private Long id;
039
040    @XmlElement(name = "name")
041    private String name;
042
043    @XmlElement(name = "display_name")
044    private String displayName;
045
046    @XmlElement(name = "description")
047    private String description;
048
049
050    @Override
051    public String toString() {
052        return "MeasuredUnit{" +
053                "id=" + id +
054                ", name='" + name + '\'' +
055                ", displayName=" + displayName +
056                ", description=" + description +
057                '}';
058    }
059
060    @Override
061    public void setHref(final Object href) {
062        super.setHref(href);
063
064        if (this.href != null) {
065            final Matcher m = MEASURED_UNITS_CODE_PATTERN.matcher(this.href);
066            if (m.find()) {
067                setId(m.group(1));
068            }
069        }
070    }
071
072    @Override
073    public boolean equals(Object o) {
074        if (this == o) return true;
075        if (o == null || getClass() != o.getClass()) return false;
076
077        MeasuredUnit measuredUnit = (MeasuredUnit) o;
078        if (id != null ? !id.equals(measuredUnit.id) : measuredUnit.id != null) return false;
079        if (name != null ? !name.equals(measuredUnit.name) : measuredUnit.name != null) return false;
080        if (displayName != null ? !displayName.equals(measuredUnit.displayName) : measuredUnit.displayName != null) return false;
081        if (description != null ? !description.equals(measuredUnit.description) : measuredUnit.description != null) return false;
082        return true;
083    }
084
085    @Override
086    public int hashCode() {
087        return Objects.hashCode(
088                id,
089                name,
090                displayName,
091                description
092        );
093    }
094
095    public Long getId() {
096        return id;
097    }
098
099    public void setId(final Object id) {
100        this.id = longOrNull(id);
101    }
102
103    public String getName() {
104        return name;
105    }
106
107    public void setName(final Object name) {
108        this.name = stringOrNull(name);
109    }
110
111    public String getDisplayName() {
112        return displayName;
113    }
114
115    public void setDisplayName(final Object displayName) {
116        this.displayName = stringOrNull(displayName);
117    }
118
119    public String getDescription() {
120        return description;
121    }
122
123    public void setDescription(final Object description) {
124        this.description = stringOrNull(description);
125    }
126}