jp.zippyzip.City.java Source code

Java tutorial

Introduction

Here is the source code for jp.zippyzip.City.java

Source

/*
 * zippyzipjp
 * 
 * Copyright 2008-2010 Michinobu Maeda.
 * 
 * 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 jp.zippyzip;

import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

/**
 * ??
 * 
 * @author Michinobu Maeda
 */
public class City {

    /** */
    protected static Logger log = Logger.getLogger(City.class.getName());

    /** ? */
    public static final Timestamp TIMESTAMP_MAX = Timestamp.valueOf("2999-12-31 23:59:59");

    /**  */
    private String code;

    /** ?? */
    private String name;

    /** ? */
    private String yomi;

    /** ? */
    private Date expiration;

    /**
     * JSON ???
     * 
     * @param json JSON
     * @return 
     */
    public static City fromJson(String json) {

        City ret = null;

        try {

            JSONObject jo = new JSONObject(json);

            ret = new City(jo.optString("code", ""), jo.optString("name", ""), jo.optString("yomi", ""),
                    Timestamp.valueOf(jo.optString("expiration", "")));

        } catch (JSONException e) {
            log.log(Level.WARNING, "", e);
        }

        return ret;
    }

    /**
     * 
     */
    public City() {
        this("", "", "", TIMESTAMP_MAX);
    }

    /**
     * ???
     * 
     * @param code 
     * @param name ??
     * @param yomi ?
     */
    public City(String code, String name, String yomi) {
        this(code, name, yomi, TIMESTAMP_MAX);
    }

    /**
     * ???
     * 
     * @param code 
     * @param name ??
     * @param yomi ?
     * @param expiration ?
     */
    public City(String code, String name, String yomi, Date expiration) {

        this.code = code;
        this.name = name;
        this.yomi = yomi;
        setExpiration(expiration);
    }

    /**
     * ???
     * 
     * @param code 
     * @param name ??
     * @param yomi ?
     * @param expiration ?
     */
    public City(String code, String name, String yomi, long expiration) {

        this.code = code;
        this.name = name;
        this.yomi = yomi;
        setExpiration(expiration);
    }

    /**
     * ??
     * 
     * @return 
     */
    public String getCode() {
        return code;
    }

    /**
     * ?
     * 
     * @param code 
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * ????
     * 
     * @return ??
     */
    public String getName() {
        return name;
    }

    /**
     * ???
     * 
     * @param name ??
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * ???
     * 
     * @return ?
     */
    public String getYomi() {
        return yomi;
    }

    /**
     * ??
     * 
     * @param yomi ?
     */
    public void setYomi(String yomi) {
        this.yomi = yomi;
    }

    /**
     * ???
     * 
     * @return ?
     */
    public Date getExpiration() {
        return expiration;
    }

    /**
     * ??
     * 
     * @param expiration ?
     */
    public void setExpiration(Date expiration) {
        this.expiration = new Timestamp(expiration.getTime());
    }

    /**
     * ??
     * 
     * @param expiration ?
     */
    public void setExpiration(long expiration) {
        this.expiration = new Timestamp(expiration);
    }

    /**
     * JSON ??
     * 
     * @returnJSON
     */
    public String toJson() {

        String ret = null;

        try {

            ret = new JSONStringer().object().key("code").value(code).key("name").value(name).key("yomi")
                    .value(yomi).key("expiration").value(expiration.toString()).endObject().toString();

        } catch (JSONException e) {
            log.log(Level.WARNING, "", e);
        }

        return ret;
    }
}