Java tutorial
/** * Copyright 2011 Masanori Matsumoto * * 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 net.uytrewq.jogp; import java.util.Collection; import java.util.Iterator; import java.util.Map; import javax.annotation.concurrent.Immutable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; /** * Ogp value container. */ @Immutable public final class OgpValues { private final String prefix; private final Multimap<String, String> values; private final String htmlTitle; private final Collection<String> htmlImages; /** * Constructor. * * @param prefix * The prefix of the ogp key name. Mostly it would be "og:" * @param values * The multimap of the ogp values. */ public OgpValues(String prefix, Multimap<String, String> values, String htmlTitle, Collection<String> htmlImages) { this.prefix = prefix; this.values = ImmutableMultimap.copyOf(values); this.htmlTitle = htmlTitle; this.htmlImages = ImmutableList.copyOf(htmlImages); } /** * Returns the prefix. * * @return The prefix. */ public String getPrefix() { return prefix; } /** * Returns native values. The returned multimap is immutable. * * @return The values. */ public Multimap<String, String> getValues() { return values; } /** * Returns the value by the key. * * @param key * The key name without prefix. * @return The value or null. If more than one values exist, it returns the * first one. If no values exist, it returns null. */ public String get(String key) { String qname = prefix + key; Collection<String> v = values.get(qname); return v.isEmpty() ? null : v.iterator().next(); } /** * Returns the title * * @return The title or null. */ public String getTitle() { return get("title"); } /** * Returns the type * * @return The type or null. */ public String getType() { return get("type"); } /** * Returns the url. * * @return The url or null. */ public String getUrl() { return get("url"); } /** * Returns the image url. * * @return The image url or null. */ public String getImage() { return get("image"); } /** * Returns the site name. * * @return The site name or null. */ public String getSiteName() { return get("site_name"); } /** * Returns the description. * * @return The description or null. */ public String getDescription() { return get("description"); } /** * Returns html title. * * @return The html title or null. */ public String getHtmlTitle() { return htmlTitle; } /** * Returns html images. * * @return The html images. */ public Collection<String> getHtmlImages() { return htmlImages; } @Override public String toString() { StringBuilder sb = new StringBuilder(this.getClass().getName()).append("@").append(this.hashCode()) .append(" {\n"); for (Map.Entry<String, Collection<String>> e : values.asMap().entrySet()) { sb.append("\t").append(e.getKey()).append(" => "); if (e.getValue().size() > 1) { sb.append("["); for (Iterator<String> it = e.getValue().iterator(); it.hasNext();) { String v = it.next(); sb.append(v); if (it.hasNext()) { sb.append(", "); } } sb.append("]"); } else { sb.append(e.getValue().iterator().next()); } sb.append("\n"); } sb.append("}"); return sb.toString(); } }