Java tutorial
/* * Copyright (C) 2012 Nicolas A. Brard-Nault * * 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.sitemap4j.sitemap.url; import static com.google.common.base.Preconditions.checkNotNull; import java.util.HashMap; import java.util.Map; import java.util.Set; import org.sitemap4j.value.AbstractValueObject; import com.google.common.collect.Sets; public class ChangeFrequency extends AbstractValueObject implements Comparable<ChangeFrequency> { @SuppressWarnings("serial") final static private Map<String, ChangeFrequency> AvailableFrequenciesTable = new HashMap<String, ChangeFrequency>() { public void add(final String asString, final Integer frequency) { put(asString, new ChangeFrequency(asString, frequency)); } { add("always", 0); add("hourly", 3600); add("daily", 3600 * 24); add("weekly", 3600 * 24 * 7); add("monthly", 3600 * 24 * 30); add("yearly", 3600 * 24 * 365); add("never", Integer.MAX_VALUE); } }; static public Set<ChangeFrequency> getAll() { return Sets.newHashSet(AvailableFrequenciesTable.values()); } private String frequencyAsString; private Integer frequencyInSeconds; private ChangeFrequency(final String frequencyAsString, final Integer frequencyInSeconds) { checkNotNull(frequencyAsString); checkNotNull(frequencyInSeconds); this.frequencyAsString = frequencyAsString; this.frequencyInSeconds = frequencyInSeconds; } @Override public int compareTo(final ChangeFrequency that) { return -1 * this.frequencyInSeconds.compareTo(that.frequencyInSeconds); } @Override public String toString() { return frequencyAsString; } public Integer getFrequencyInSeconds() { return frequencyInSeconds; } static public ChangeFrequency fromString(final String frequencyAsString) { if (!AvailableFrequenciesTable.containsKey(frequencyAsString)) { throw new UnknownChangeFrequency(frequencyAsString, AvailableFrequenciesTable.keySet()); } return AvailableFrequenciesTable.get(frequencyAsString); } static public ChangeFrequency always() { return fromString("always"); } static public ChangeFrequency hourly() { return fromString("hourly"); } static public ChangeFrequency daily() { return fromString("daily"); } static public ChangeFrequency weekly() { return fromString("weekly"); } static public ChangeFrequency monthly() { return fromString("monthly"); } static public ChangeFrequency yearly() { return fromString("yearly"); } static public ChangeFrequency never() { return fromString("never"); } }