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

com.dubture.getcomposer.core.collection.Dependencies Maven / Gradle / Ivy

package com.dubture.getcomposer.core.collection;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import org.json.simple.JSONObject;

import com.dubture.getcomposer.core.VersionedPackage;
import com.dubture.getcomposer.core.entities.AbstractJsonObject;

/**
 * Represents a dependencies collection of a composer package, either require or
 * require-dev
 * 
 * @see http://getcomposer.org/doc/04-schema.md#require
 * @see http://getcomposer.org/doc/04-schema.md#require-dev
 * @author Thomas Gossmann 
 */
public class Dependencies extends AbstractJsonObject implements Iterable {
	
	public Dependencies() {
	}
	
	@SuppressWarnings("unchecked")
	protected void parse(Object obj) {
		clear();
		if (obj instanceof JSONObject) {
			for (Entry entry : ((Map)((JSONObject)obj)).entrySet()) {
				VersionedPackage dep = new VersionedPackage();
				dep.setName(entry.getKey());
				dep.setVersion((String)entry.getValue());
				add(dep);
			}
		}
	}
	
	public Object prepareJson(LinkedList fields) {
		LinkedHashMap out = new LinkedHashMap();
		for (VersionedPackage dep : this) {
			out.put(dep.getName(), dep.getVersion());
		}
		return out;
	}
	
	/**
	 * Adds a new dependency.
	 * 
	 * @param dependency the new dependency
	 * @return this
	 */
	public void add(VersionedPackage dependency) {
		if (!has(dependency)) {
			set(dependency.getName(), dependency);
		}
	}
	
	public void addAll(Dependencies dependencies) {
		for (VersionedPackage pkg : dependencies) {
			add(pkg);
		}
	}
	
	
	public boolean has(VersionedPackage dependency) {
		return super.has(dependency.getName());
		// TODO: matches version, e.g. ~1 matches 1.0.1 but not 2.* and >1 matches 1.0.1 and 2.*
//		if (super.has(dependency.getName())) {
//			return get(dependency.getName()).getVersion().equals(dependency.getVersion());
//		}
//		
//		return false;
	}

	/**
	 * Removes a dependency.
	 * 
	 * @param dependency the dependency to remove
	 */
	public void remove(VersionedPackage dependency) {
		super.remove(dependency.getName());
	}
	
	public VersionedPackage[] toArray() {
		return properties.values().toArray(new VersionedPackage[]{});
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Iterable#iterator()
	 */
	public Iterator iterator() {
		return (Iterator)properties.values().iterator();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy