/**
 * AbstractTitleProvider.java
 * Created May 20, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.title.provider.internal;

import java.util.Collection;
import java.util.HashSet;

import com.tandbergtv.watchpoint.pmm.title.provider.ITitleProvider;
import com.tandbergtv.watchpoint.pmm.title.provider.ITitleProviderProperties;
import com.tandbergtv.watchpoint.pmm.title.provider.ITitleSearchStrategy;


/**
 * Abstract implementation of the {@link ITitleProvider} interface.
 * 
 * @author Sahil Verma
 */
public abstract class AbstractTitleProvider implements ITitleProvider {
	
	protected String systemId;
	
	protected String name;
	
	protected ITitleSearchStrategy strategy;
	
	protected Collection<String> specifications;
	
	protected ClassLoader classLoader;

	/**
	 * Constructor
	 * @param properties Title Provider properties
	 */
	protected AbstractTitleProvider(ITitleProviderProperties properties) {
		this(properties, null);
	}
	
	/**
	 * Constructor
	 * @param properties Title Provider properties
	 */
	protected AbstractTitleProvider(ITitleProviderProperties properties, ClassLoader classLoader) {
		this.systemId = properties.getSystemID();
		this.name = properties.getName();
		this.setSearchStrategy(properties.getSearchStrategy());
		this.specifications = new HashSet<String>();
		if (properties.getSupportedSpecifications() != null)
			this.specifications.addAll(properties.getSupportedSpecifications());
		this.classLoader = classLoader;
	}
	
	/**
	 * Creates a AbstractTitleProvider
	 * @param systemId
	 * @param name
	 * @param strategy
	 */
	protected AbstractTitleProvider(String systemId, String name, String specification) {
		this.systemId = systemId;
		this.name = name;
		this.specifications = new HashSet<String>();
		this.specifications.add(specification);
	}

	/**
	 * Creates a AbstractTitleProvider
	 * @param systemId
	 * @param name
	 * @param strategy
	 * @param specifications
	 */
	protected AbstractTitleProvider(String systemId, String name, ITitleSearchStrategy strategy,
			Collection<String> specifications) {
		this.systemId = systemId;
		this.name = name;
		this.setSearchStrategy(strategy);
		this.specifications = specifications;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.provider.ITitleProvider#getName()
	 */
	public String getName() {
		return this.name;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.provider.ITitleProvider#getSearchStrategy()
	 */
	public ITitleSearchStrategy getSearchStrategy() {
		return this.strategy;
	}

	/* Set the strategy for this title provider */
	protected void setSearchStrategy(ITitleSearchStrategy strategy) {
		this.strategy = strategy;
		if (strategy != null)
			strategy.setTitleProvider(this);
	}
	
	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.provider.ITitleProvider#getSupportedSpecifications()
	 */
	public Collection<String> getSupportedSpecifications() {
		return this.specifications;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.provider.ITitleProvider#getSystemID()
	 */
	public String getSystemID() {
		return this.systemId;
	}

	/* (non-Javadoc)
	 * @see com.tandbergtv.watchpoint.pmm.title.provider.ITitleProvider#supportsSpecification(java.lang.String)
	 */
	public boolean supportsSpecification(String specification) {
		return specifications.contains(specification);
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "[" + systemId + "] " + name;
	}	
}
