import java.util.Iterator;
import java.util.HashMap;


public class AlignedPARCLIPread implements Comparable<AlignedPARCLIPread> {
	private char _strand;
	private long _startPosition;
	private long _endPosition;
	private int _readCount;
	private byte _numberOfMismatches;
    //	private HashMap<Byte, Integer> _conversionMap = new HashMap<Byte, Integer>();
    private HashMap<Byte, Integer> _conversionMap = new HashMap<Byte, Integer>(0); //MR02012018    
	
	public AlignedPARCLIPread(char strand, long startPosition, long endPosition, int readCount, byte numberOfMismatches, HashMap<Byte, Integer> conversionMap) {
		_strand = strand;
		_startPosition = startPosition;
		_endPosition = endPosition;
		_readCount = readCount;
		_numberOfMismatches = numberOfMismatches;
		_conversionMap = conversionMap;
	}
	
	public char getStrand() {
		return _strand;
	}
	public long getStartPosition() {
		return _startPosition;
	}
	public long getEndPosition() {
		return _endPosition;
	}
	public int getReadCount() {
		return _readCount;
	}
	public int getLength() {
		return (int) ( _endPosition - _startPosition + 1 );
	}
	public HashMap<Byte, Integer> getConversionMap() {
		return _conversionMap;
	}
	public byte getNumberOfMismatches() {
		return _numberOfMismatches;
	}
	
	public void addCount( int count ) {
		_readCount += count;
	}
	public void incorporateMap( HashMap<Byte, Integer> map ) {
		Iterator<Byte> incIt = map.keySet().iterator();
		while( incIt.hasNext() ) {
			Byte location = incIt.next();
			if( _conversionMap.containsKey(location) ) {
				_conversionMap.put(location, _conversionMap.get(location) + map.get(location) );
			}
			else {
				_conversionMap.put(location, map.get(location));
			}
		}
	}
	
	
	
//***************************************************************			
// For Comparing...	
	@Override
	public int hashCode() {
		return Integer.parseInt(_strand + Long.toString( (_startPosition*31) + (_endPosition*29) ));
	}
	@Override
	public boolean equals( Object other ) {
		if( other == null ) { return false; }
		if( other == this ) { return true; }
		if( this.getClass() != other.getClass() ) { return false; }
		AlignedPARCLIPread otherLocation = (AlignedPARCLIPread) other;
		if( otherLocation.getStrand() == this.getStrand() 
				&& otherLocation.getStartPosition() == this.getStartPosition()
				&& otherLocation.getEndPosition() == this.getEndPosition()
		) {
			return true;
		}
		else { return false; }
	}
//***************************************************************		
//***********************************************************************	
// For sorting
	@Override
	public int compareTo(AlignedPARCLIPread read2) {
		if( read2.getStrand() == this.getStrand() ) {
			if( read2.getStartPosition() == this.getStartPosition() ) {
				return (int) ( this.getEndPosition() - read2.getEndPosition() );
			}
			else {
				return (int) ( this.getStartPosition() - read2.getStartPosition() );
			}
		}
		else if ( read2.getStrand() == '-' ) { return -1; }
		return 1;
	}
//***********************************************************************	

}
