jdk/test/jdk/lambda/shapegen/TTNode.java
changeset 29784 b9220497eb43
parent 29783 db33e568f107
parent 29770 6415d011ad02
child 29785 da950f343762
equal deleted inserted replaced
29783:db33e568f107 29784:b9220497eb43
     1 /*
       
     2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 package shapegen;
       
    25 
       
    26 import shapegen.ClassCase.Kind;
       
    27 
       
    28 import java.util.ArrayList;
       
    29 import java.util.List;
       
    30 import java.util.Set;
       
    31 
       
    32 import static shapegen.ClassCase.Kind.*;
       
    33 
       
    34 /**
       
    35  * Type Template Node
       
    36  *
       
    37  * @author Robert Field
       
    38  */
       
    39 public class TTNode {
       
    40 
       
    41     final List<TTNode> supertypes;
       
    42     final boolean canBeClass;
       
    43 
       
    44     private int currentKindIndex;
       
    45     private Kind[] kinds;
       
    46 
       
    47     public TTNode(List<TTNode> subtypes, boolean canBeClass) {
       
    48         this.supertypes = subtypes;
       
    49         this.canBeClass = canBeClass;
       
    50     }
       
    51 
       
    52     public void start(boolean includeClasses) {
       
    53         kinds =
       
    54              supertypes.isEmpty()?
       
    55                 (new Kind[]{IDEFAULT, IPRESENT})
       
    56              :  ((includeClasses && canBeClass)?
       
    57                   new Kind[]{CNONE, IVAC, IDEFAULT, IPRESENT}
       
    58                 : new Kind[]{IVAC, IDEFAULT, IPRESENT});
       
    59         currentKindIndex = 0;
       
    60 
       
    61         for (TTNode sub : supertypes) {
       
    62             sub.start(includeClasses);
       
    63         }
       
    64     }
       
    65 
       
    66     public boolean next() {
       
    67         ++currentKindIndex;
       
    68         if (currentKindIndex >= kinds.length) {
       
    69             currentKindIndex = 0;
       
    70             return false;
       
    71         } else {
       
    72             return true;
       
    73         }
       
    74     }
       
    75 
       
    76     public void collectAllSubtypes(Set<TTNode> subs) {
       
    77         subs.add(this);
       
    78         for (TTNode n : supertypes) {
       
    79             n.collectAllSubtypes(subs);
       
    80         }
       
    81     }
       
    82 
       
    83     private Kind getKind() {
       
    84         return kinds[currentKindIndex];
       
    85     }
       
    86 
       
    87     boolean isInterface() {
       
    88         return getKind().isInterface;
       
    89     }
       
    90 
       
    91     boolean isClass() {
       
    92         return !isInterface();
       
    93     }
       
    94 
       
    95     boolean hasDefault() {
       
    96         return getKind() == IDEFAULT;
       
    97     }
       
    98 
       
    99     public boolean isValid() {
       
   100         for (TTNode n : supertypes) {
       
   101             if (!n.isValid() || (isInterface() && n.isClass())) {
       
   102                 return false;
       
   103             }
       
   104         }
       
   105         return true;
       
   106     }
       
   107 
       
   108     public ClassCase genCase() {
       
   109         ClassCase subclass;
       
   110         List<TTNode> ttintfs;
       
   111         if (isClass() && !supertypes.isEmpty() && supertypes.get(0).isClass()) {
       
   112             subclass = supertypes.get(0).genCase();
       
   113             ttintfs = supertypes.subList(1, supertypes.size());
       
   114         } else {
       
   115             subclass = null;
       
   116             ttintfs = supertypes;
       
   117         }
       
   118         List<ClassCase> intfs = new ArrayList<>();
       
   119         for (TTNode node : ttintfs) {
       
   120             intfs.add(node.genCase());
       
   121         }
       
   122         return new ClassCase(getKind(), subclass, intfs);
       
   123     }
       
   124 }