summaryrefslogtreecommitdiff
path: root/Homework/cs5300/project-three/parser/util/SymbolComparator.java
blob: e131c799f451b3351e00b86da87467bedf1bc6f2 (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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package parser.util;

import java.util.Comparator;
import java.util.HashSet;

/**
 *
 * @author edwajohn
 */
public class SymbolComparator implements Comparator<String> {

  private final HashSet<String> terminals;

  public SymbolComparator(HashSet<String> terminals) {
    this.terminals = terminals;
  }

  @Override
  public int compare(String o1, String o2) {
    // Slightly less efficient code for clarity.
    if (terminals.contains(o1) && terminals.contains(o2)) {
      return o1.compareTo(o2);
    }
    if (terminals.contains(o1)) {
      return 1;
    }
    if (terminals.contains(o2)) {
      return -1;
    }
    return o1.compareTo(o2);
  }

}