All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.api.tools.framework.model.Element Maven / Gradle / Ivy

There is a newer version: 0.0.8
Show newest version
/*
 * Copyright (C) 2016 Google Inc.
 *
 * 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.google.api.tools.framework.model;

import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.inject.Key;

import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

/**
 * Base class for all model elements.
 *
 * 

* Elements allow to attach dynamic attributes based on a typed key, which are typically established * by a {@link Processor}. The decision whether to use a dynamic attribute or a plain getter/setter * which is hard-wired into the object model depends on whether the expressed information is * globally relevant for many processor or only used locally by a tool. For example, derived type * information is considered to be globally relevant and hard-wired into the object model, so it is * easy discoverable. */ public abstract class Element { private final Map, Object> attributes = Maps.newHashMap(); /** * Returns the model associated with this element. */ public abstract Model getModel(); /** * Returns the location associated with this element. */ public abstract Location getLocation(); /** * Returns a fully qualified name for the element. */ public abstract String getFullName(); /** * Returns a simple name for the element. */ public abstract String getSimpleName(); /** * Puts an attribute value, returning the old one. */ @SuppressWarnings("unchecked") @Nullable public T putAttribute(Key key, T value) { return (T) attributes.put(key, Preconditions.checkNotNull(value)); } /** * Adds an attribute value. */ @SuppressWarnings("unchecked") public void addAttribute(Key> key, T value) { List list = getAttribute(key); if (list == null) { list = Lists.newArrayList(); putAttribute(key, list); } list.add(value); } /** * Removes an attribute value, returning the old one. */ @SuppressWarnings("unchecked") @Nullable public T removeAttribute(Key key) { return (T) attributes.remove(key); } /** * Gets an attribute value. */ @SuppressWarnings("unchecked") public T getAttribute(Key key) { return (T) attributes.get(key); } /** * Gets an attribute value if it exists, otherwise the given default value. */ @SuppressWarnings("unchecked") public T getAttributeOrDefault(Key key, T defaultValue) { T value = (T) attributes.get(key); if (value != null) { return value; } return defaultValue; } /** * Checks whether an attribute is present. */ @Nullable public boolean hasAttribute(Key key) { return attributes.containsKey(key); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy