001/* 002 * Shredzone Commons 003 * 004 * Copyright (C) 2012 Richard "Shred" Körber 005 * http://commons.shredzone.org 006 * 007 * This program is free software: you can redistribute it and/or modify 008 * it under the terms of the GNU Library General Public License as 009 * published by the Free Software Foundation, either version 3 of the 010 * License, or (at your option) any later version. 011 * 012 * This program is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU Library General Public License 018 * along with this program. If not, see <http://www.gnu.org/licenses/>. 019 */ 020 021package org.shredzone.commons.view; 022 023import java.util.HashMap; 024import java.util.HashSet; 025import java.util.Map; 026import java.util.Set; 027 028import javax.annotation.ParametersAreNonnullByDefault; 029 030/** 031 * An abstract implementation of {@link PathContext}. Extending classes need to offer 032 * getters and setters for their properties. If a setter is used, the property name needs 033 * to be passed to {@link #addProperty(String)}. In return, this class will take care 034 * for creating a {@link Signature} instance. 035 * 036 * @author Richard "Shred" Körber 037 */ 038@ParametersAreNonnullByDefault 039public abstract class AbstractPathContext implements PathContext { 040 041 private final Set<String> propSet = new HashSet<>(); 042 private final Map<String, Object> variables = new HashMap<>(); 043 044 @Override 045 public Signature getSignature() { 046 return new Signature(propSet); 047 } 048 049 @Override 050 public Map<String, Object> getVariables() { 051 return variables; 052 } 053 054 /** 055 * Sets a variable. 056 * 057 * @param name 058 * Variable name 059 * @param value 060 * Variable value 061 */ 062 public void setVariable(String name, Object value) { 063 addProperty('#' + name); 064 variables.put(name, value); 065 } 066 067 /** 068 * Adds a property name to the signature. Subclasses must pass the property name to 069 * this method for every relevant property setter that was invoked. It is safe to call 070 * this method with the same property name multiple times. 071 * 072 * @param propName 073 * name of the property that was set. 074 */ 075 protected void addProperty(String propName) { 076 propSet.add(propName); 077 } 078 079}