Java tutorial
package com.conwet.silbops.model.basic; /* * #%L * SilboPS API * %% * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ import java.util.Objects; import org.json.simple.JSONValue; import com.conwet.silbops.util.JSONizable; /** * This class model the attribute concept: a name with a type. * * @author sergio */ public class Attribute implements JSONizable { private String name; private Type type; /** * Construct an attribute with the given parameters * @param name the name of the attribute * @param type the type of the attribute * @throws IllegalArgumentException if some of the parameters are null */ public Attribute(String name, Type type) { this.name = Objects.requireNonNull(name, "Attribute name is null"); this.type = Objects.requireNonNull(type, "Type is null"); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Attribute) { Attribute other = (Attribute) obj; return type == other.type && name.equals(other.name); } return false; } @Override public int hashCode() { return 41 * type.hashCode() + name.hashCode(); } /** * @return the attribute name */ public String getName() { return name; } /** * @return the attribute type */ public Type getType() { return type; } @Override public String toString() { return "[name=" + name + ", type=" + type + "]"; } /** * Convert the json in an attribute object. * @param json the json to convert * @return the attribute object */ public static Attribute fromJSON(String json) { String[] fields = json.split(":"); return new Attribute(fields[0], Type.fromJSON(fields[1])); } @Override public String toJSON() { return name + ":" + type.toJSON(); } @Override public String toJSONString() { return JSONValue.toJSONString(this.toJSON()); } }