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

spoon.pattern.internal.ResultHolder 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.pattern.internal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
import java.util.function.Function;

import spoon.SpoonException;

/**
 * Container for single or multiple values of required type
 */
public abstract class ResultHolder {
	private final Class requiredClass;

	protected ResultHolder(Class requiredClass) {
		this.requiredClass = requiredClass;
	}

	/**
	 * @return the class of values, which acceptable by this result holder
	 */
	public Class getRequiredClass() {
		return requiredClass;
	}

	/**
	 * @return true if it accepts 0, 1 or more values. false if it accepts exactly one value. If none, then value is null
	 */
	public abstract boolean isMultiple();

	/**
	 * adds a result into this result holder
	 * @param value a new value of result holder
	 */
	public abstract void addResult(T value);

	/**
	 * calls consumer.accept(value) once for each contained value
	 * @param consumer
	 */
	public abstract void mapEachResult(Function consumer);
	/**
	 * @return List of actually stored results
	 */
	public abstract List getResults();

	/**
	 * Container of single value of required type
	 *
	 * @param 
	 */
	public static class Single extends ResultHolder {

		private T result;

		public Single(Class requiredClass) {
			super(requiredClass);
		}

		@Override
		public boolean isMultiple() {
			return false;
		}

		@Override
		public void addResult(T value) {
			if (this.result != null) {
				throw new SpoonException("Cannot add second value into single value ConversionContext");
			}
			this.result = value;
		}

		public T getResult() {
			return result;
		}

		@Override
		public void mapEachResult(Function consumer) {
			result = consumer.apply(result);
		}

		@Override
		public List getResults() {
			return result == null ? Collections.emptyList() : Collections.singletonList(result);
		}
	}

	/**
	 * Container of multiple values of required type
	 *
	 * @param 
	 */
	public static class Multiple extends ResultHolder {

		List result = new ArrayList<>();

		public Multiple(Class requiredClass) {
			super(requiredClass);
		}

		@Override
		public boolean isMultiple() {
			return true;
		}
		@Override
		public void addResult(T value) {
			this.result.add(value);
		}

		public List getResult() {
			return result;
		}

		@Override
		public List getResults() {
			return result;
		}

		@Override
		public void mapEachResult(Function consumer) {
			for (ListIterator iter = result.listIterator(); iter.hasNext();) {
				iter.set(consumer.apply(iter.next()));
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy