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

com.regnosys.rosetta.common.util.RosettaObjectCollectorProcessStep Maven / Gradle / Ivy

Go to download

Rune Common is a java library that is utilised by Rosetta Code Generators and models expressed in the Rosetta DSL.

There is a newer version: 11.31.0
Show newest version
package com.regnosys.rosetta.common.util;

/*-
 * ==============
 * Rune Common
 * ==============
 * Copyright (C) 2018 - 2024 REGnosys
 * ==============
 * 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.
 * ==============
 */

import com.rosetta.lib.postprocess.PostProcessorReport;
import com.rosetta.model.lib.RosettaModelObject;
import com.rosetta.model.lib.path.RosettaPath;
import com.rosetta.model.lib.process.AttributeMeta;
import com.rosetta.model.lib.process.Processor.Report;
import com.rosetta.model.lib.process.PostProcessStep;

import java.util.ArrayList;
import java.util.List;

public class RosettaObjectCollectorProcessStep implements PostProcessStep {

	private final Class collectRosettaType;

	public RosettaObjectCollectorProcessStep(Class collectRosettaType) {
		this.collectRosettaType = collectRosettaType;
	}

	@Override
	public Integer getPriority() {
		return 3;
	}

	@Override
	public String getName() {
		return "RosettaObjectCollector postProcessor";
	}

	@Override
	public  RosettaObjectCollectorProcessReport runProcessStep(Class topClass, T instance) {
		List collectedObjects = new ArrayList<>();
		RosettaObjectCollectorProcess process = new RosettaObjectCollectorProcess<>(collectRosettaType, collectedObjects);
		RosettaPath path = RosettaPath.valueOf(instance.getType().getSimpleName());
		process.processRosetta(path, topClass, instance, null);
		instance.process(path, process);
		return new RosettaObjectCollectorProcessReport<>(instance, collectedObjects);
	}

	private static class RosettaObjectCollectorProcess extends SimpleProcessor {

		private final Class collectObjectType;
		private final List collectedObjects;

		RosettaObjectCollectorProcess(Class collectObjectType, List collectedObjects) {
			this.collectObjectType = collectObjectType;
			this.collectedObjects = collectedObjects;
		}

		@Override
		public  boolean processRosetta(RosettaPath path, Class rosettaType, R instance,
				RosettaModelObject parent, AttributeMeta... metas) {
			if (instance == null) {
				return false;
			}
			if (collectObjectType.isInstance(instance)) {
				collectedObjects.add(collectObjectType.cast(instance));
			}
			return true;
		}

		@Override
		public Report report() {
			return null;
		}
	}

	public static class RosettaObjectCollectorProcessReport implements PostProcessorReport, Report {

		private final RosettaModelObject topClass;
		private final List collectedObjects;

		RosettaObjectCollectorProcessReport(RosettaModelObject topClass, List collectedObjects) {
			this.topClass = topClass;
			this.collectedObjects = collectedObjects;
		}

		@Override
		public RosettaModelObject getResultObject() {
			return topClass;
		}

		public List getCollectedObjects() {
			return collectedObjects;
		}
	}
}