it.scoppelletti.programmerpower.wui.DefaultActivity.java Source code

Java tutorial

Introduction

Here is the source code for it.scoppelletti.programmerpower.wui.DefaultActivity.java

Source

/*
 * Copyright (C) 2012 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.scoppelletti.programmerpower.wui;

import java.io.*;
import java.util.*;
import org.springframework.beans.factory.annotation.*;
import it.scoppelletti.programmerpower.*;
import it.scoppelletti.programmerpower.io.*;
import it.scoppelletti.programmerpower.reflect.*;
import it.scoppelletti.programmerpower.types.*;

/**
 * Implementazione di default dell&rsquo;interfaccia {@code Activity}.
 *
 * @since 1.0.0
 */
@Final
public class DefaultActivity implements Activity {
    private static final long serialVersionUID = 1L;

    /**
     * @serial Titolo.
     */
    private String myTitle;

    /**
     * @serial URL dell&rsquo;attivit&agrave;.
     */
    private String myActivityUrl;

    /**
     * @serial URL dell&rsquo;icona.
     */
    private String myIconUrl;

    private transient Set<String> myCategories;

    /**
     * Costruttore.
     */
    public DefaultActivity() {
    }

    /**
     * Serializza l&rsquo;oggetto.
     * 
     * @param      out Flusso di scrittura.
     * @serialData     Formato di default seguito dall&rsquo;elenco delle
     *                 categorie:
     *
     * <P><OL>
     * <LI>Numero di categorie.
     * <LI>Sequenza di categorie.
     * </OL></P>
     */
    private void writeObject(ObjectOutputStream out) throws IOException {
        out.defaultWriteObject();

        if (myCategories == null) {
            out.writeInt(-1);
        } else {
            out.writeInt(myCategories.size());
            for (String catg : myCategories) {
                out.writeObject(catg);
            }
        }
    }

    /**
     * Deserializza l&rsquo;oggetto.
     * 
     * @param in Flusso di lettura.
     */
    private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
        int i, n;
        String catg;

        in.defaultReadObject();

        try {
            if (Strings.isNullOrEmpty(myTitle)) {
                throw new PropertyNotSetException(getClass().getName(), "title");
            }
            if (Strings.isNullOrEmpty(myActivityUrl)) {
                throw new PropertyNotSetException(getClass().getName(), "activityURL");
            }
            if (Strings.isNullOrEmpty(myIconUrl)) {
                throw new PropertyNotSetException(getClass().getName(), "iconURL");
            }

            n = in.readInt();
            myCategories = new HashSet<String>(1);
            for (i = 0; i < n; i++) {
                catg = (String) in.readObject();
                myCategories.add(catg);
            }
        } catch (Exception ex) {
            throw new DeserializeException(ex);
        }
    }

    public String getTitle() {
        return myTitle;
    }

    /**
     * Imposta l&rsquo;URL dell&rsquo;attivit&agrave;.
     * 
     * @param value Valore.
     * @see it.scoppelletti.programmerpower.wui.Activity#getTitle    
     */
    @Required
    public void setTitle(String value) {
        if (Strings.isNullOrEmpty(value)) {
            throw new ArgumentNullException("value");
        }

        myTitle = value;
    }

    public String getActivityUrl() {
        return myActivityUrl;
    }

    /**
     * Imposta l&rsquo;URL dell&rsquo;attivit&agrave;.
     * 
     * @param url Valore.
     * @see it.scoppelletti.programmerpower.wui.Activity#getActivityUrl    
     */
    @Required
    public void setActivityUrl(String url) {
        if (Strings.isNullOrEmpty(url)) {
            throw new ArgumentNullException("url");
        }

        myActivityUrl = url;
    }

    public String getIconUrl() {
        return myIconUrl;
    }

    /**
     * Imposta l&rsquo;URL dell&rsquo;icona.
     * 
     * @param url Valore.
     * @see it.scoppelletti.programmerpower.wui.Activity#getIconUrl
     */
    @Required
    public void setIconUrl(String url) {
        if (Strings.isNullOrEmpty(url)) {
            throw new ArgumentNullException("url");
        }

        myIconUrl = url;
    }

    public Collection<String> getCategories() {
        if (myCategories == null) {
            myCategories = new HashSet<String>(1);
        }

        return myCategories;
    }

    /**
     * Imposta le categorie.
     * 
     * @param obj Collezione.
     * @see it.scoppelletti.programmerpower.wui.Activity#getCategories
     */
    public void setCategories(Collection<String> obj) {
        if (obj == null) {
            throw new ArgumentNullException("obj");
        }

        myCategories = new HashSet<String>(obj);
    }
}