sandbox/java.hints/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/pm/NFA.java
branchdonation_review
changeset 1043 57843026e60b
parent 1027 205b7632914c
parent 1040 f7b6892fd754
child 1044 7feb751ba76b
     1.1 --- a/sandbox/java.hints/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/pm/NFA.java	Mon Dec 19 11:37:36 2016 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,216 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2009-2010 Oracle and/or its affiliates. All rights reserved.
     1.8 - *
     1.9 - * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 - * Other names may be trademarks of their respective owners.
    1.11 - *
    1.12 - * The contents of this file are subject to the terms of either the GNU
    1.13 - * General Public License Version 2 only ("GPL") or the Common
    1.14 - * Development and Distribution License("CDDL") (collectively, the
    1.15 - * "License"). You may not use this file except in compliance with the
    1.16 - * License. You can obtain a copy of the License at
    1.17 - * http://www.netbeans.org/cddl-gplv2.html
    1.18 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 - * specific language governing permissions and limitations under the
    1.20 - * License.  When distributing the software, include this License Header
    1.21 - * Notice in each file and include the License file at
    1.22 - * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 - * particular file as subject to the "Classpath" exception as provided
    1.24 - * by Oracle in the GPL Version 2 section of the License file that
    1.25 - * accompanied this code. If applicable, add the following below the
    1.26 - * License Header, with the fields enclosed by brackets [] replaced by
    1.27 - * your own identifying information:
    1.28 - * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 - *
    1.30 - * If you wish your version of this file to be governed by only the CDDL
    1.31 - * or only the GPL Version 2, indicate your decision by adding
    1.32 - * "[Contributor] elects to include this software in this distribution
    1.33 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 - * single choice of license, a recipient has the option to distribute
    1.35 - * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 - * to extend the choice of license to its licensees as provided above.
    1.37 - * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 - * Version 2 license, then the option applies only if the new code is
    1.39 - * made subject to such option by the copyright holder.
    1.40 - *
    1.41 - * Contributor(s):
    1.42 - *
    1.43 - * Portions Copyrighted 2009 Sun Microsystems, Inc.
    1.44 - */
    1.45 -
    1.46 -package org.netbeans.modules.java.hints.spiimpl.pm;
    1.47 -
    1.48 -import java.util.BitSet;
    1.49 -import java.util.HashSet;
    1.50 -import java.util.Map;
    1.51 -import java.util.Set;
    1.52 -
    1.53 -/**
    1.54 - *
    1.55 - * @author lahvac
    1.56 - */
    1.57 -public class NFA<I, R> {
    1.58 -
    1.59 -    /*XXX: private*/ final int stateCount;
    1.60 -    private final int startingState;
    1.61 -    private final Set<I> inputs;
    1.62 -    private final Map<Key<I>, State> transitionTable;
    1.63 -    private final Map<Integer, R> finalStates;
    1.64 -
    1.65 -    private final State startingStateObject;
    1.66 -
    1.67 -    private NFA(int startingState, int stateCount, Set<I> inputs, Map<Key<I>, State> transitionTable, Map<Integer, R> finalStates) {
    1.68 -        this.startingState = startingState;
    1.69 -        this.stateCount = stateCount;
    1.70 -        this.inputs = inputs;
    1.71 -        this.transitionTable = transitionTable;
    1.72 -        this.finalStates = finalStates;
    1.73 -
    1.74 -        startingStateObject = State.create().mutableOr(startingState);
    1.75 -    }
    1.76 -
    1.77 -    public State getStartingState() {
    1.78 -        return startingStateObject;
    1.79 -    }
    1.80 -
    1.81 -    public State transition(final State active, final I input) {
    1.82 -        State result = null;
    1.83 -
    1.84 -//        for (int i : active) {
    1.85 -        for (int i = active.nextSetBit(0); i >= 0; i = active.nextSetBit(i+1)) {
    1.86 -             State target = transitionTable.get(Key.create(i, input));
    1.87 -
    1.88 -             if (target != null) {
    1.89 -                 if (result == null) {
    1.90 -                     result = State.create();
    1.91 -                 }
    1.92 -                 
    1.93 -                 result.mutableOr(target);
    1.94 -             }
    1.95 -        }
    1.96 -
    1.97 -        State r;
    1.98 -
    1.99 -        //XXX:
   1.100 -        if (result == null) {
   1.101 -            r = startingStateObject;
   1.102 -        } else {
   1.103 -            r = result.mutableOr(startingState);//???
   1.104 -        }
   1.105 -
   1.106 -        return r;
   1.107 -    }
   1.108 -
   1.109 -    public Set<R> getResults(State bs) {
   1.110 -        Set<R> result = new HashSet<R>();
   1.111 -
   1.112 -        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
   1.113 -            if (finalStates.get(i) != null) {
   1.114 -                result.add(finalStates.get(i));
   1.115 -            }
   1.116 -        }
   1.117 -
   1.118 -        return result;
   1.119 -    }
   1.120 -
   1.121 -    public static <I, R> NFA<I, R> create(int startingState, int stateCount, Set<I> inputs, Map<Key<I>, State> transitionTable, Map<Integer, R> finalStates) {
   1.122 -        return new NFA<I, R>(startingState, stateCount, inputs, transitionTable, finalStates);
   1.123 -    }
   1.124 -
   1.125 -    public State join(State s1, State s2) {
   1.126 -        State bs = State.create();
   1.127 -
   1.128 -        bs.mutableOr(s1);
   1.129 -        bs.mutableOr(s2);
   1.130 -
   1.131 -        return bs;
   1.132 -    }
   1.133 -
   1.134 -    public static final class Key<I> {
   1.135 -        private final int state;
   1.136 -        private final I   input;
   1.137 -
   1.138 -        private Key(int state, I input) {
   1.139 -            this.state = state;
   1.140 -            this.input = input;
   1.141 -        }
   1.142 -
   1.143 -        public static <I> Key<I> create(int state, I input) {
   1.144 -            return new Key<I>(state, input);
   1.145 -        }
   1.146 -
   1.147 -        @Override
   1.148 -        public boolean equals(Object obj) {
   1.149 -            if (obj == null) {
   1.150 -                return false;
   1.151 -            }
   1.152 -            if (getClass() != obj.getClass()) {
   1.153 -                return false;
   1.154 -            }
   1.155 -            final Key<?> other = (Key<?>) obj;
   1.156 -            if (this.state != other.state) {
   1.157 -                return false;
   1.158 -            }
   1.159 -            if (this.input != other.input && (this.input == null || !this.input.equals(other.input))) {
   1.160 -                return false;
   1.161 -            }
   1.162 -            return true;
   1.163 -        }
   1.164 -
   1.165 -        @Override
   1.166 -        public int hashCode() {
   1.167 -            int hash = 3;
   1.168 -            hash = 83 * hash + this.state;
   1.169 -            hash = 83 * hash + (this.input != null ? this.input.hashCode() : 0);
   1.170 -            return hash;
   1.171 -        }
   1.172 -
   1.173 -        @Override
   1.174 -        public String toString() {
   1.175 -            return "[" + state + ", " + input + "]";
   1.176 -        }
   1.177 -
   1.178 -    }
   1.179 -
   1.180 -//    public static final class State extends HashSet<Integer> {
   1.181 -//        private State() {
   1.182 -//        }
   1.183 -//
   1.184 -//        public static State create() {
   1.185 -//            return new State();
   1.186 -//        }
   1.187 -//
   1.188 -//        public State mutableOr(int state) {
   1.189 -//            add(state);
   1.190 -//            return this;
   1.191 -//        }
   1.192 -//
   1.193 -//        public State mutableOr(State or) {
   1.194 -//            addAll(or);
   1.195 -//            return this;
   1.196 -//        }
   1.197 -//
   1.198 -//    }
   1.199 -
   1.200 -    public static final class State extends BitSet {
   1.201 -        private State() {}
   1.202 -
   1.203 -        public static State create() {
   1.204 -            return new State();
   1.205 -        }
   1.206 -
   1.207 -        public State mutableOr(int state) {
   1.208 -            set(state);
   1.209 -            return this;
   1.210 -        }
   1.211 -
   1.212 -        public State mutableOr(State or) {
   1.213 -            or(or);
   1.214 -            return this;
   1.215 -        }
   1.216 -
   1.217 -    }
   1.218 -
   1.219 -}