Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: db80e77facbee7a3a8531444d921d1f5faa5ad28 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*******************************************************************************
 * Copyright (c) 2010 Oracle Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Oracle Corporation - initial API and implementation and/or initial documentation
 *******************************************************************************/

package org.eclipse.jst.jsf.common.metadata.query.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jst.jsf.common.metadata.Entity;
import org.eclipse.jst.jsf.common.metadata.Trait;
import org.eclipse.jst.jsf.common.metadata.query.AbstractTraitQueryVisitor;
import org.eclipse.jst.jsf.common.metadata.query.IResultSet;
import org.eclipse.jst.jsf.common.metadata.query.MetaDataException;
import org.eclipse.jst.jsf.common.metadata.query.internal.IPredicateMatcher.MATCH;

/**
 * Visits a set of entities matching by trait predicate matcher
 *
 */
public class TraitQuerySpecVisitor extends AbstractTraitQueryVisitor {

	private List<Trait> 					_traitResults;
	private Collection<Entity> 				_entities;
	private IPredicateMatcher<Trait> 		_matcher;
	private SearchControl 					_control;
	private boolean 						_stop;
	

	/**
	 * Constructor using default search control
	 * @param entities
	 * @param traitMatcher
	 */
	public TraitQuerySpecVisitor(final Collection<Entity> entities, final IPredicateMatcher<Trait> traitMatcher) {
		this(entities, traitMatcher, new SearchControl());
	}

	/**
	 * Constructor using search control for a single match
	 * @param entities
	 * @param traitMatcher
	 * @param control
	 */
	public TraitQuerySpecVisitor(final Collection<Entity> entities, final IPredicateMatcher<Trait> traitMatcher, final SearchControl control) {
		super();
		_entities = entities;
		_matcher = traitMatcher;
		_traitResults = new ArrayList<Trait>();
		_control = control;
	}

	/**
	 * @return IResultSet<Trait> - will not be null
	 * @throws MetaDataException
	 */
	public IResultSet<Trait> findTraits() throws MetaDataException {
		
		_matcher.reset();
		
		if (_entities != null && _entities.size() > 0){
			ENTITIES_LOOP: for (final Entity entity : _entities) {
				final Iterator<Trait> it = entity.getTraits().iterator();
				while (it.hasNext()) {
					Trait trait = it.next();
					trait.accept(this);
					checkShouldStopVisiting();
					if (stopVisiting())
						break ENTITIES_LOOP;
				}
			}			
		}		
		
		return new SimpleResultSet(_traitResults);
	}

	public boolean stopVisiting() {
		return _stop;
	}

	private void checkShouldStopVisiting(){
		//implement how to set stop to signal to the entity accept() to skip visiting
		if (_stop == false
				&& _control.getCountLimit()== _traitResults.size() 
				&& _control.getCountLimit() != SearchControl.COUNT_LIMIT_NONE )
			
			_stop = true;
	}

	@Override
	public void visit(final Trait trait) {
		final MATCH match = _matcher.matches(trait);
		if (match == MATCH.FULLY) {
			_traitResults.add(trait);			
		}
	}

}

Back to the top