Code example for TreeMap

Methods: containsKey, firstKey, get, headMap, isEmpty, lastKey, put, tailMap

0
	public boolean addEvent(int time, Event event) {
		// check first 
		if (!checkTime(time, event.getLengthInSeconds())) return false;
		 
		// otherwise add event 
		events.put(new Integer(time), event);
		return true; 
	} 
	 
	/** 
	 * Checks whether a certain time slot is free 
	 * @param time 
	 * @param length in seconds 
	 * @return false, if time slot is not free 
	 */ 
	public boolean checkTime(int time, int length) {
		// if empty set, you can add stuff 
		if (events.isEmpty()) return true;
		 
		// lowest or highest keys? 
		int lowest = events.firstKey();
		if (lowest > time) { // there is no key before?
			if (time + length > lowest) return false; // too long
			else return true; // ok 
		} 
		int highest = events.lastKey();
		if (highest < time + length) return true;
 
		// ok, we are in between somewhere - check event before 
		Integer beforeKey = floorKey(time);
		int endTime = beforeKey + events.get(beforeKey).getLengthInSeconds();
		if (endTime > time) return false;
 
		// check event after 
		int after = ceilingKey(beforeKey + 1); // next event after before key
		if (time + length > after) return false;
 
		return true; 
	} 
	 
	/** 
	 * return the next event for a given time 
	 * @param time 
	 * @return map.entry 
	 */ 
	 
	public Entry<Integer, Event> getNextEvent(int time) {
		// last key? 
		if (time > events.lastKey()) return null;
		 
		HashMap<Integer, Event> map = new HashMap<Integer, Event>(1);
		map.put(ceilingKey(time), events.get(ceilingKey(time)));
		return map.entrySet().iterator().next();
	}	 
	 
	/** 
	 * returns the entry set itself 
	 * @return Set<Map.Entry<Integer,Event>> set of events 
	 */ 
	public Set<Map.Entry<Integer,Event>> getEntrySet() {
		return events.entrySet();
	} 
 
	/** 
	 * Prints list of missions 
	 */ 
	@Override 
	public String toString() {
		StringBuilder builder = new StringBuilder();
		// get values 
		for(Map.Entry<Integer,Event> entry : events.entrySet()) {
			int time = entry.getKey();
			builder.append(EventList.formatTime(time));
			builder.append(" - ");
			builder.append(entry.getValue().getDescription(time));
			builder.append('\n');
		} 
		return builder.toString();
	} 
	 
    /** 
     * Gets the entry corresponding to the specified key; if no such entry 
     * exists, returns the entry for the greatest key less than the specified 
     * key; if no such entry exists, returns {@code null}. 
     */ 
	/* 
    final Entry<Integer,Event> floorEntry(Integer time) { 
    	 
    	// Since this is not in till API level 9 for android going this is a dirty version. 
    	for(Entry<Integer, Event> entry : events.headMap(time).entrySet()) { 
    		return entry; 
    	} 
		return null;    	 
    } 
    */ 
     
    /** 
     * Gets the entry corresponding to the specified key; if no such entry 
     * exists, returns the entry for the greatest key less than the specified 
     * key; if no such entry exists, returns {@code null}. 
     */ 
    final Integer floorKey(Integer time) {
    	 
    	if(events.containsKey(time)) {
    		return time;
    	} 
    	 
    	SortedMap<Integer, Event> map = events.headMap(time);    	
    	return map.isEmpty() ? null : map.lastKey();    	
    }     
     
    /** 
     *	Returns the least key greater than or equal to the given key,  
     *	or null if there is no such key. 
     */ 
    final Integer ceilingKey(Integer time) {
    	 
    	if(events.containsKey(time)) {
    		return time;
    	} 
    	 
    	SortedMap<Integer, Event> map = events.tailMap(time);    	
    	return map.isEmpty() ? null : map.firstKey();  
    } 
}