summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/analysis/SourceIndex.java
blob: de1630876c6399b6c024d86f857f8060cb3277a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*******************************************************************************
 * Copyright (c) 2014 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.analysis;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import jsyntaxpane.Token;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import cuchaz.enigma.mapping.Entry;

public class SourceIndex implements Iterable<Map.Entry<Entry,Token>>
{
	private Multimap<Entry,Token> m_entryToTokens;
	
	public SourceIndex( )
	{
		m_entryToTokens = HashMultimap.create();
	}
	
	public void add( Entry entry, Token token )
	{
		m_entryToTokens.put( entry, token );
	}
	
	public Iterator<Map.Entry<Entry,Token>> iterator( )
	{
		return m_entryToTokens.entries().iterator();
	}
	
	public Collection<Token> tokens( )
	{
		return m_entryToTokens.values();
	}
	
	public Entry getEntry( Token token )
	{
		// linear search is fast enough for now
		for( Map.Entry<Entry,Token> entry : this )
		{
			if( entry.getValue().equals( token ) )
			{
				return entry.getKey();
			}
		}
		return null;
	}
	
	public Map.Entry<Entry,Token> getEntry( int pos )
	{
		// linear search is fast enough for now
		for( Map.Entry<Entry,Token> entry : this )
		{
			Token token = entry.getValue();
			if( pos >= token.start && pos <= token.end() )
			{
				return entry;
			}
		}
		return null;
	}
	
	public Collection<Token> getTokens( Entry entry )
	{
		return m_entryToTokens.get( entry );
	}
}