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

com.emc.mongoose.api.model.io.task.BasicIoTaskBuilder Maven / Gradle / Ivy

There is a newer version: 4.0.0-alpha5
Show newest version
package com.emc.mongoose.api.model.io.task;

import com.emc.mongoose.api.common.supply.BatchSupplier;
import com.emc.mongoose.api.common.supply.ConstantStringSupplier;
import com.emc.mongoose.api.model.item.Item;
import com.emc.mongoose.api.model.io.IoType;
import com.emc.mongoose.api.model.storage.Credential;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 Created by kurila on 14.07.16.
 */
public class BasicIoTaskBuilder>
implements IoTaskBuilder {
	
	protected final int originCode = hashCode();
	
	protected IoType ioType = IoType.CREATE; // by default
	protected String inputPath = null;
	
	protected BatchSupplier outputPathSupplier;
	protected boolean constantOutputPathFlag;
	protected String constantOutputPath;
	
	protected BatchSupplier uidSupplier;
	protected boolean constantUidFlag;
	protected String constantUid;
	
	protected BatchSupplier secretSupplier;
	protected boolean constantSecretFlag;
	protected String constantSecret;
	
	protected Map credentialsMap = null;
	
	@Override
	public final int getOriginCode() {
		return originCode;
	}
	
	@Override
	public final IoType getIoType() {
		return ioType;
	}

	@Override
	public final BasicIoTaskBuilder setIoType(final IoType ioType) {
		this.ioType = ioType;
		return this;
	}

	public final String getInputPath() {
		return inputPath;
	}

	@Override
	public final BasicIoTaskBuilder setInputPath(final String inputPath) {
		this.inputPath = inputPath;
		return this;
	}
	
	@Override
	public final BasicIoTaskBuilder setOutputPathSupplier(final BatchSupplier ops) {
		this.outputPathSupplier = ops;
		if(outputPathSupplier == null) {
			constantOutputPathFlag = true;
			constantOutputPath = null;
		} else if(outputPathSupplier instanceof ConstantStringSupplier) {
			constantOutputPathFlag = true;
			constantOutputPath = outputPathSupplier.get();
		} else {
			constantOutputPathFlag = false;
		}
		return this;
	}
	
	@Override
	public final BasicIoTaskBuilder setUidSupplier(final BatchSupplier uidSupplier) {
		this.uidSupplier = uidSupplier;
		if(uidSupplier == null) {
			constantUidFlag = true;
			constantUid = null;
		} else if(uidSupplier instanceof ConstantStringSupplier) {
			constantUidFlag = true;
			constantUid = uidSupplier.get();
		} else {
			constantUidFlag = false;
		}
		return this;
	}
	
	@Override
	public final BasicIoTaskBuilder setSecretSupplier(
		final BatchSupplier secretSupplier
	) {
		this.secretSupplier = secretSupplier;
		if(secretSupplier == null) {
			constantSecretFlag = true;
			constantSecret = null;
		} else if(secretSupplier instanceof ConstantStringSupplier) {
			constantSecretFlag = true;
			constantSecret = secretSupplier.get();
		} else {
			constantSecretFlag = false;
		}
		return this;
	}
	
	@Override
	public BasicIoTaskBuilder setCredentialsMap(final Map credentials) {
		if(credentials != null) {
			this.credentialsMap = credentials;
			setSecretSupplier(null);
		}
		return this;
	}
	
	@Override @SuppressWarnings("unchecked")
	public O getInstance(final I item)
	throws IOException {
		final String uid;
		return (O) new BasicIoTask<>(
			originCode, ioType, item, inputPath, getNextOutputPath(),
			Credential.getInstance(uid = getNextUid(), getNextSecret(uid))
		);
	}

	@Override @SuppressWarnings("unchecked")
	public void getInstances(final List items, final List buff)
	throws IOException {
		String uid;
		for(final I item : items) {
			buff.add(
				(O) new BasicIoTask<>(
					originCode, ioType, item, inputPath, getNextOutputPath(),
					Credential.getInstance(uid = getNextUid(), getNextSecret(uid))
				)
			);
		}
	}
	
	protected final String getNextOutputPath() {
		return constantOutputPathFlag ? constantOutputPath : outputPathSupplier.get();
	}
	
	protected final String getNextUid() {
		return constantUidFlag ? constantUid : uidSupplier.get();
	}
	
	protected final String getNextSecret(final String uid) {
		if(uid != null && credentialsMap != null) {
			return credentialsMap.get(uid);
		} else if(constantSecretFlag) {
			return constantSecret;
		} else {
			return secretSupplier.get();
		}
	}
	
	@Override
	public void close()
	throws IOException {
		inputPath = null;
		if(outputPathSupplier != null) {
			outputPathSupplier.close();
			outputPathSupplier = null;
		}
		if(uidSupplier != null) {
			uidSupplier.close();
			uidSupplier = null;
		}
		if(secretSupplier != null) {
			secretSupplier.close();
			secretSupplier = null;
		}
		if(credentialsMap != null) {
			credentialsMap.clear();
			credentialsMap = null;
		}
	}
}