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

spoon.reflect.meta.impl.SingleHandler Maven / Gradle / Ivy

Go to download

Spoon is a tool for meta-programming, analysis and transformation of Java programs.

There is a newer version: 11.1.1-beta-14
Show newest version
/*
 * SPDX-License-Identifier: (MIT OR CECILL-C)
 *
 * Copyright (C) 2006-2023 INRIA and contributors
 *
 * Spoon is available either under the terms of the MIT License (see LICENSE-MIT.txt) or the Cecill-C License (see LICENSE-CECILL-C.txt). You as the user are entitled to choose the terms under which to adopt Spoon.
 */
package spoon.reflect.meta.impl;

import java.util.AbstractList;
import java.util.Collections;

import spoon.SpoonException;
import spoon.reflect.meta.ContainerKind;
import spoon.reflect.meta.RoleHandler;
import spoon.reflect.path.CtRole;

/**
 * implementation of {@link RoleHandler}
 * @param  the type of node whose attribute has to be manipulated
 * @param  the type of value of the attribute
 */
abstract class SingleHandler extends AbstractRoleHandler {

	protected SingleHandler(CtRole role, Class targetType, Class valueClass) {
		super(role, targetType, valueClass);
	}

	@Override
	public ContainerKind getContainerKind() {
		return ContainerKind.SINGLE;
	}

	@Override
	public  java.util.Collection asCollection(W element) {
		return asList(element);
	}

	@Override
	public  java.util.List asList(W e) {
		return new AbstractList() {
			T element = castTarget(e);
			boolean hasValue = SingleHandler.this.getValue(element) != null;

			@Override
			public int size() {
				return hasValue ? 1 : 0;
			}

			@SuppressWarnings("unchecked")
			@Override
			public X get(int index) {
				if (index < 0 || index >= size()) {
					throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
				}
				return (X) SingleHandler.this.getValue(element);
			}

			@Override
			public X set(int index, X value) {
				if (index < 0 || index >= size()) {
					throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
				}
				X oldValue = get(0);
				SingleHandler.this.setValue(element, value);
				return oldValue;
			}

			@Override
			public boolean add(X value) {
				if (hasValue) {
					//single value cannot have more then one value
					throw new SpoonException("Single value attribute cannot have more then one value");
				}
				SingleHandler.this.setValue(element, value);
				hasValue = true;
				return true;
			}

			@Override
			public X remove(int index) {
				if (index < 0 || index >= size()) {
					throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size());
				}
				X oldValue = get(0);
				if (oldValue != null) {
					SingleHandler.this.setValue(element, null);
				}
				hasValue = false;
				return oldValue;
			}

			@Override
			public boolean remove(Object value) {
				if (!hasValue) {
					return false;
				}
				X oldValue = get(0);
				if (equals(oldValue, value)) {
					if (oldValue != null) {
						SingleHandler.this.setValue(element, null);
					}
					hasValue = false;
					return true;
				}
				return false;
			}

			private boolean equals(Object v1, Object v2) {
				if (v1 == v2) {
					return true;
				}
				if (v1 == null) {
					return false;
				}
				return v1.equals(v2);
			}
		};
	}

	@Override
	public  java.util.Set asSet(W element) {
		return Collections.singleton(getValue(element));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy