1 /*
   2  * Copyright (c) 2005, 2015, 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 jdk.test.lib.jittester.utils;
  25 
  26 import jdk.test.lib.jittester.BuiltInType;
  27 import jdk.test.lib.jittester.Type;
  28 
  29 import java.util.Collection;
  30 import java.util.List;
  31 import java.util.stream.Collectors;
  32 
  33 /**
  34  * Utility functions for type system
  35  */
  36 public class TypeUtil {
  37     /**
  38      * Gets a list of implicitly castable types to a given one from the collection of types
  39      *
  40      * @param types a collection to get types from
  41      * @param type  a target type which result type could be implicitly cast to
  42      * @return      a result collection of types that match given conditions
  43      */
  44     public static Collection<Type> getImplicitlyCastable(Collection<Type> types, Type type) {
  45         return types.stream()
  46                 .filter(t -> t.canImplicitlyCastTo(type))
  47                 .collect(Collectors.toList());
  48     }
  49 
  50     /**
  51      * Gets a list of explicitly castable types to a given one from the collection of types
  52      *
  53      * @param types a collection to get types from
  54      * @param type  a target type which result type could be explicitly cast to
  55      * @return      a result collection of types that match given conditions
  56      */
  57     public static Collection<Type> getExplicitlyCastable(Collection<Type> types, Type type) {
  58         return types.stream()
  59                 .filter(t -> t.canExplicitlyCastTo(type))
  60                 .collect(Collectors.toList());
  61     }
  62 
  63     /**
  64      * Gets a list of more capacious types than a given one from the collection of types
  65      *
  66      * @param types a collection to get types from
  67      * @param type  a type to filter given types by capacity
  68      * @return      a result collection of types that match given conditions
  69      */
  70     public static List<Type> getMoreCapaciousThan(Collection<Type> types, BuiltInType type) {
  71         return types.stream()
  72                 .filter(t -> ((BuiltInType) t).isMoreCapaciousThan(type))
  73                 .collect(Collectors.toList());
  74     }
  75 
  76     /**
  77      * Gets a list of less or equal capacious types than a given one from the collection of types
  78      *
  79      * @param types a collection to get types from
  80      * @param type  a type to filter given types by capacity
  81      * @return      a result collection of types that match given conditions
  82      */
  83     public static List<Type> getLessCapaciousOrEqualThan(Collection<Type> types, BuiltInType type) {
  84         return types.stream()
  85                 .filter(t -> !((BuiltInType) t).isMoreCapaciousThan(type) || t.equals(type))
  86                 .collect(Collectors.toList());
  87     }
  88 }