Java tutorial
/* * Copyright 2011 Objectos, Fbrica de Software LTDA. * * 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 br.com.objectos.blog; import java.util.Collection; import org.joda.time.DateTime; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import com.google.common.collect.Multimap; /** * @author marcio.endo@objectos.com.br (Marcio Endo) */ class PostTemplateImpl implements PostTemplate { private final Document doc; private final Multimap<PostTag, Element> tags; public PostTemplateImpl(Builder builder) { this.doc = builder.getDoc(); this.tags = builder.getTags(); } @Override public void setPostId(String postId) { tryToSet(PostTag.POST_ID, postId); } @Override public void setTitle(String title) { tryToSet(PostTag.TITLE, title); } @Override public void setAuthor(String author) { tryToSet(PostTag.AUTHOR, author); } @Override public void setDate(DateTime date) { DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm"); tryToSet(PostTag.DATE, formatter.print(date)); } @Override public void setContents(String contents) { tryToSet(PostTag.CONTENTS, contents); } @Override public String toString() { return doc.toString(); } private void tryToSet(PostTag tag, String text) { Collection<Element> elements = tags.get(tag); for (Element thisElement : elements) { if (thisElement != null) { tag.replace(thisElement, text); } } } }