com.dyuproject.protostuff.parser.AnnotationContainer Maven / Gradle / Ivy
The newest version!
//========================================================================
//Copyright 2007-2010 David Yu [email protected]
//------------------------------------------------------------------------
//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 com.dyuproject.protostuff.parser;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Base class for components that contain annotations.
*
* @author David Yu
* @created Dec 30, 2010
*/
public abstract class AnnotationContainer implements HasAnnotations, HasProto
{
final LinkedHashMap annotations =
new LinkedHashMap();
public void add(Annotation annotation)
{
if(annotations.put(annotation.name, annotation) != null)
throw err("Duplicate annotation: " + annotation.name, null);
}
public Map getAnnotationMap()
{
return annotations;
}
/**
* Short-hand for {@link #getAnnotationMap()}.
*
* You then can use:
*
* <if(message.a.("SomeAnnotation"))>
*
*/
public final Map getA()
{
return annotations;
}
public Collection getAnnotations()
{
return annotations.values();
}
public Annotation getAnnotation(String name)
{
return annotations.get(name);
}
public boolean addAnnotations(Map source, boolean clearSource)
{
if(source.isEmpty())
return false;
this.annotations.putAll(source);
if(clearSource)
source.clear();
return true;
}
/**
* Shorthand for annotations.isEmpty().
*
*
* You can then use:
* <if(message.emptyA)>
*
*
*
* Note that this does not work on stringtemplate:
* <if(message.annotationMap.empty)>
*
* Even though {@link java.util.Map#isEmpty()} exists.
*
*
*
*/
public final boolean isEmptyA()
{
return annotations.isEmpty();
}
public static IllegalStateException err(String msg, Proto proto)
{
if(proto == null)
return new IllegalStateException(msg);
return new IllegalStateException(msg + " [" + proto.getSourcePath() + "]");
}
}