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

com.github.gentity.ToManySide Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2019 The Gentity Project.
 *
 * 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.github.gentity;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;

/**
 *
 * @author count
 */
public abstract class ToManySide, O> extends RelationSide{
	
	final Function collectionProvider;

	private ToManySide(Function collectionProvider) {
		this.collectionProvider = collectionProvider;
	}
	
	public static  ToManySide, O> of(Function> listProvider, RelationSide other) {
		ToManySide, O> instance = of(listProvider);
		instance.connect(other);
		return instance;
	}
	
	public static  ToManySide, O> of(Function> listProvider) {
		return new ToManySide, O>(listProvider) {
			@Override
			public List get(T host) {
				RelationSide other = getOther();
				if(other != null) {
					return new ListWrapper(collectionProvider.apply(host), host, getOther());
				} else {
					return collectionProvider.apply(host);
				}
			}
		};
	}

	
	@Override
	public RelationSide bind(T thisSide, O otherSide) {
		if(thisSide == null) {
			// We silently ignore binding to null.
			return this;
		}
		collectionProvider
			.apply(thisSide)
			.add(otherSide);
		return this;
	}

	@Override
	public RelationSide unbind(T thisSide, O otherSide) {
		if(thisSide == null) {
			// We silently ignore unbinding from null.
			return this;
		}
		collectionProvider
			.apply(thisSide)
			.remove(otherSide);
		return this;
	}

	public abstract C get(T host);
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy