• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Libphonenumber Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.i18n.phonenumbers.metadata.regex;
18 
19 import com.google.auto.value.AutoValue;
20 
21 /**
22  * Value type for nodes in NFA graphs of phone number regular expressions. This is basically a
23  * trivial wrapper for an {@code int}, but it makes a lot of other pieces of code type safe.
24  * Outside this package, this type is mainly used for examining NFA graphs which represent a
25  * regular expression, generated via {@link RangeTreeConverter#toNfaGraph}.
26  */
27 @AutoValue
28 public abstract class Node implements Comparable<Node> {
29   /** The unique initial node in an NFA graph with in-order zero. */
30   public static final Node INITIAL = new AutoValue_Node(0);
31   /** The unique terminal node in an NFA graph with out-order zero. */
32   public static final Node TERMINAL = new AutoValue_Node(1);
33 
34   /** Returns a new node whose ID is one greater than this node. */
createNext()35   public Node createNext() {
36     return (id() == 0) ? TERMINAL : new AutoValue_Node(id() + 1);
37   }
38 
39   /** Returns the numeric ID of this node, which must be unique within an NFA graph. */
id()40   abstract int id();
41 
42   @Override
compareTo(Node o)43   public int compareTo(Node o) {
44     return Integer.compare(id(), o.id());
45   }
46 
47   @Override
toString()48   public final String toString() {
49     return Integer.toString(id());
50   }
51 }
52