Java tutorial
package com.conwet.silbops.model; /* * #%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.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.AbstractMap.SimpleImmutableEntry; import java.util.AbstractSet; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map.Entry; import java.util.Objects; import java.util.Set; import org.json.simple.JSONArray; import com.conwet.silbops.model.basic.Attribute; import com.conwet.silbops.model.basic.Type; import com.conwet.silbops.util.JSONizable; import com.conwet.silbops.util.externalizer.AttributeExternalizer; /** * Advertise represents a {@linkplain Set} of {@linkplain Attribute} * * @author sergio * @apiviz.owns com.conwet.silbops.model.basic.Attribute */ public class Advertise implements JSONizable, Externalizable, IterableAttribute<Attribute> { private static final long serialVersionUID = 1L; private static final AttributeExternalizer externalizer; static { externalizer = new AttributeExternalizer(); } private Set<Attribute> attributes; /** * Creates an empty Advertise */ public Advertise() { attributes = new HashSet<>(); } /** * Create an attribute entry with the given name and type. * It doesn't accept nulls. * * @param name the name of the attribute to add. * @param type the attribute {@link Type}. * @return The same object to enable chaining. */ public Advertise attribute(String name, Type type) { return attribute(new Attribute(name, type)); } /** * Create an attribute entry with the given attribute * @param attribute the new attribute to add, it doesn't insert nulls. * @return the same object to enable chaining. */ public Advertise attribute(Attribute attribute) { attributes.add(Objects.requireNonNull(attribute, "Attribute is null")); return this; } /** * @return a {@link Set} view of the current attributes. */ @Override public Set<Attribute> getAttributes() { return Collections.unmodifiableSet(attributes); } /** * @throws UnsupportedOperationException since there is no real need to * duplicate elements. */ @Override public Set<Entry<Attribute, Attribute>> entries() { return new AbstractSet<Entry<Attribute, Attribute>>() { @Override public Iterator<Entry<Attribute, Attribute>> iterator() { final Iterator<Attribute> iterator = attributes.iterator(); return new Iterator<Entry<Attribute, Attribute>>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override public Entry<Attribute, Attribute> next() { Attribute attribute = iterator.next(); return new SimpleImmutableEntry<>(attribute, attribute); } @Override public void remove() { throw new UnsupportedOperationException("Read-only iterator."); } }; } @Override public int size() { return attributes.size(); } }; } /** * @return the size of the advertise: it is the sum of the dimensions of * each attribute. */ public int size() { return attributes.size(); } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj instanceof Advertise) { Advertise other = (Advertise) obj; return attributes.equals(other.attributes); } return false; } @Override public int hashCode() { return 43 * attributes.hashCode(); } @Override public String toString() { return attributes.toString(); } /** * Creates a new Advertise instance using the attributes of the given iterable * * @param iterable the object to read attributes * @return an advertise with the same attribute of iterable */ public static Advertise asAdvertise(IterableAttribute<?> iterable) { Advertise advertise = new Advertise(); for (Attribute attribute : iterable.getAttributes()) { advertise.attribute(attribute); } return advertise; } @Override public void readExternal(ObjectInput input) throws IOException, ClassNotFoundException { Set<Attribute> attrSet = new HashSet<>(); int size = input.readInt(); for (int i = 0; i < size; i++) { attrSet.add(externalizer.readExternal(input)); } attributes = attrSet; } /** * @serialData writes the number of attributes and their content. */ @Override public void writeExternal(ObjectOutput output) throws IOException { output.writeInt(attributes.size()); for (Attribute attribute : attributes) { externalizer.writeExternal(output, attribute); } } /** * Deserializes a Advertise from its JSON representation. * * @param json JSON representation of a notification * @return A new notification */ @SuppressWarnings("unchecked") public static Advertise fromJSON(JSONArray json) { Advertise advertise = new Advertise(); for (String attribute : (List<String>) json) { advertise.attribute(Attribute.fromJSON(attribute)); } return advertise; } @Override @SuppressWarnings("unchecked") public JSONArray toJSON() { JSONArray json = new JSONArray(); for (Attribute attribute : attributes) { json.add(attribute.toJSON()); } return json; } @Override public String toJSONString() { return toJSON().toJSONString(); } }