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

spoon.reflect.meta.impl.SetHandler 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.AbstractSet;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

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

/**
 * implementation of {@link RoleHandler}, which handles attributes of type Set<V>
 * @param  the type of node whose attribute has to be manipulated
 * @param  the type of item value of the attribute
 */
abstract class SetHandler extends AbstractRoleHandler, V> {

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

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

	@Override
	protected Set castValue(Object value) {
		Set set = super.castValue(value);
		//check that each item has expected class
		checkItemsClass(set);
		return set;
	}

	@Override
	public  Collection asCollection(W element) {
		return asSet(element);
	}

	@Override
	public  Set asSet(W e) {
		return new AbstractSet() {
			T element = castTarget(e);

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public Iterator iterator() {
				return (Iterator) SetHandler.this.iterator(element);
			}

			@Override
			public int size() {
				return SetHandler.this.size(element);
			}

			@Override
			public boolean contains(Object o) {
				return SetHandler.this.contains(element, o);
			}

			@Override
			public boolean add(X value) {
				return SetHandler.this.add(element, castItemValue(value));
			}

			@Override
			public boolean remove(Object value) {
				return SetHandler.this.remove(element, value);
			}
		};
	}

	protected boolean remove(T element, Object value) {
		Set values = new HashSet<>(this.>getValue(element));
		boolean ret = values.remove(value);
		if (ret) {
			setValue(element, values);
		}
		return false;
	}

	protected boolean add(T element, V value) {
		Set values = new HashSet<>(this.>getValue(element));
		boolean ret = values.add(value);
		if (ret) {
			setValue(element, values);
		}
		return ret;
	}

	protected boolean contains(T element, Object o) {
		return this.>getValue(element).contains(o);
	}

	protected int size(T element) {
		return this.>getValue(element).size();
	}

	protected Iterator iterator(T element) {
		return this.>getValue(element).iterator();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy