Java tutorial
/***************************************************************************** * Copyright 2013 Tobias Wich * * 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 org.electrologic.convergence.server.cache; import java.text.DateFormat; import java.util.Date; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; import org.json.JSONException; import org.json.JSONObject; /** * Base class for a cache entry representing one distinct timeframe a certificate on a remote server remained constant. * Implementations of this interface must be immutable, so that results returned by the {@link Cache} implementation can * not change in the time between the cache query and the response delivery. * * @author Tobias Wich <tobias.wich@ecsec.de> */ @Immutable @ThreadSafe public abstract class CacheEntry { /** * Gets the time in UNIX time format since when the certificate represented by this instance has been seen by this * server. * * @return Time since when the certificate has been seen. */ @Nonnegative public abstract long getStart(); /** * Gets the time in UNIX time format until when the certificate represented by this instance has been seen by this * server. * This value also marks the last query made to the remote server. This means the time value does not indicate that * the certificate has changed on the server. This information is available implicitly through the list of cache * entries. * * @return Time until when the certificate has been seen. */ @Nonnegative public abstract long getFinish(); /** * Gets the fingerprint identifying the certificate represented by this entry. * * @return Fingerprint identifying the certificate. */ @Nonnull public abstract String getFingerprint(); /** * Converts the entry to a JSON object suitable for inclusion in the fingerprint list returned in the response. * * @return JSON object with the values represented by this instance. * @throws JSONException Thrown in case the entry contains invalid elements. */ @Nonnull public JSONObject toJSON() throws JSONException { JSONObject obj = new JSONObject(); JSONObject timestamp = new JSONObject(); timestamp.put("start", getStart()); timestamp.put("finish", getFinish()); obj.put("timestamp", timestamp); obj.put("fingerprint", getFingerprint()); return obj; } @Override public String toString() { StringBuilder b = new StringBuilder(50); DateFormat dateFormatter = DateFormat.getInstance(); Date startDate = new Date(getStart()); Date finishDate = new Date(getFinish()); b.append(dateFormatter.format(startDate)); b.append(" -> "); b.append(dateFormatter.format(finishDate)); b.append(" :: ["); b.append(getFingerprint()); b.append("]"); return b.toString(); } }