org.apache.commons.collections4.Transformer Java Examples

The following examples show how to use org.apache.commons.collections4.Transformer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** gt, greater than. */
public static Transformer<Duo<Object, Object>, Boolean> gt()
{
   return new Transformer<Duo<Object, Object>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<Object, Object> d)
      {
         if (involvesNumbers(d))
         {
            return Number.class.cast(d.getA()).doubleValue() >
                   Number.class.cast(d.getB()).doubleValue();
         }
         if (involvesDates(d))
         {
            Duo<Calendar, Calendar> duo = asDuoOfCalendar(d);
            return duo.getA().after(duo.getB());
         }
         throw new IllegalStateException(notComparableMsg(d));
      }
   };
}
 
Example #2
Source File: ExecutableExpressionTree.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Creates a Node from a Transformer<Trio<?,?,?>,?>.
 * @param <T> Type of the Collection entry.
 * @param <R> Return type.
 * @param t a tri-transformer that accepts a <?,?,?> and returns a <?>.
 * @param sub1 first  Node at `depth+1` in the tree.
 * @param sub2 second Node at `depth+1` in the tree.
 * @param sub3 third  Node at `depth+1` in the tree.
 * @return a new Node.
 */
public static <T,R> Node<T,R> createNode(final Transformer<Trio<Object,Object,Object>,R> t,
      final Node<T,?> sub1, final Node<T,?> sub2, final Node<T,?> sub3)
{
   return new Node<T,R>()
   {
      @Override
      public R exec(T element)
      {
         Object o1 = sub1.exec(element);
         Object o2 = sub2.exec(element);
         Object o3 = sub3.exec(element);
         return t.transform(new Trio<>(o1, o2, o3));
      }
   };
}
 
Example #3
Source File: GroupCountArrayAndTransformerTest.java    From feilong-core with Apache License 2.0 6 votes vote down vote up
/**
 * Test group count null predicate.
 */
@Test
public void testGroupCountNullPredicate(){
    List<User> list = toList(//
                    new User("张飞", 20),
                    new User("关羽", 30),
                    new User("赵云", 50),
                    new User("刘备", 40),
                    new User("刘备", 30),
                    new User("赵云", 50));

    Map<String, Map<Object, Integer>> map = AggregateUtil
                    .groupCount(list, toArray("name"), (Map<String, Transformer<Object, Object>>) null);
    assertThat(
                    map.get("name"),
                    allOf(//
                                    hasEntry((Object) "刘备", 2),
                                    hasEntry((Object) "赵云", 2),
                                    hasEntry((Object) "张飞", 1),
                                    hasEntry((Object) "关羽", 1)));
}
 
Example #4
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** substring(string, int).  */
public static Transformer<Duo<String, Integer>, String> substring()
{
   return new Transformer<Duo<String, Integer>, String>()
   {
      @Override
      public String transform(Duo<String, Integer> u)
      {
         if (u == null || u.getA() == null)
         {
            return null;
         }
         return u.getA().substring(u.getB());
      }
   };
}
 
Example #5
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** trim. */
public static Transformer<String, String> trim()
{
   return new Transformer<String, String>()
   {
      @Override
      public String transform(String u)
      {
         if (u == null)
         {
            return null;
         }
         return u.trim();
      }
   };
}
 
Example #6
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** tolower. */
public static Transformer<String, String> tolower()
{
   return new Transformer<String, String>()
   {
      @Override
      public String transform(String u)
      {
         if (u == null)
         {
            return null;
         }
         return u.toLowerCase();
      }
   };
}
 
Example #7
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** substring(string, int, int). */
public static Transformer<Trio<String, Integer, Integer>, String> substring2()
{
   return new Transformer<Trio<String, Integer, Integer>, String>()
   {
      @Override
      public String transform(Trio<String, Integer, Integer> u)
      {
         if (u == null || u.getA() == null)
         {
            return null;
         }
         return u.getA().substring(u.getB(), u.getC() - u.getB());
      }
   };
}
 
Example #8
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** indexof. */
public static Transformer<Duo<String, String>, Integer> indexof()
{
   return new Transformer<Duo<String, String>, Integer>()
   {
      @Override
      public Integer transform(Duo<String, String> u)
      {
         if (u == null || u.getA() == null)
         {
            return -1;
         }
         return u.getA().indexOf(u.getB());
      }
   };
}
 
Example #9
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** length. */
public static Transformer<String, Integer> length()
{
   return new Transformer<String, Integer>()
   {
      @Override
      public Integer transform(String u)
      {
         if (u == null)
         {
            return 0;
         }
         return u.length();
      }
   };
}
 
Example #10
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** startswith. */
public static Transformer<Duo<String, String>, Boolean> startswith()
{
   return new Transformer<Duo<String, String>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<String, String> u)
      {
         if (u == null || u.getA() == null)
         {
            return false;
         }
         return u.getA().startsWith(u.getB());
      }
   };
}
 
Example #11
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** endswith. */
public static Transformer<Duo<String, String>, Boolean> endswith()
{
   return new Transformer<Duo<String, String>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<String, String> u)
      {
         if (u == null || u.getA() == null)
         {
            return false;
         }
         return u.getA().endsWith(u.getB());
      }
   };
}
 
Example #12
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** substringof. */
public static Transformer<Duo<String, String>, Boolean> substringof()
{
   return new Transformer<Duo<String, String>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<String, String> u)
      {
         if (u == null || u.getA() == null)
         {
            return false;
         }
         return u.getA().contains(u.getB());
      }
   };
}
 
Example #13
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** le, lower or equals than. */
public static <T> Transformer<Duo<Object, Object>, Boolean> le()
{
   return new Transformer<Duo<Object, Object>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<Object, Object> u)
      {
         if (involvesNumbers(u))
         {
         return Number.class.cast(u.getA()).doubleValue() <=
                Number.class.cast(u.getB()).doubleValue();
         }
         if (involvesDates(u))
         {
            Duo<Calendar, Calendar> duo = asDuoOfCalendar(u);
            return duo.getA().before(duo.getB()) || duo.getA().equals(duo.getB());
         }
         throw new IllegalStateException(notComparableMsg(u));
      }
   };
}
 
Example #14
Source File: ConnectionMap.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns a Map of connections from a Map of AccessInformation.
 * Uses a decorator ({@link TransformedMap}.
 * @param map to decorate.
 * @return decorator.
 */
private static Map<UUID, Connection> mapTransform(final Map<UUID, AccessInformation> map)
{
   return new TransformedMap<>
   (
         map,

         new Transformer<Duo<UUID, AccessInformation>, Connection>()
         {
            @Override
            public Connection transform(Duo<UUID, AccessInformation> u)
            {
               return new Connection(u.getA(), u.getB());
            }
         }
   );
}
 
Example #15
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/** lt, lower than. */
public static <T> Transformer<Duo<Object, Object>, Boolean> lt()
{
   return new Transformer<Duo<Object, Object>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<Object, Object> u)
      {
         if (involvesNumbers(u))
         {
         return Number.class.cast(u.getA()).doubleValue() <
                Number.class.cast(u.getB()).doubleValue();
         }
         if (involvesDates(u))
         {
            Duo<Calendar, Calendar> duo = asDuoOfCalendar(u);
            return duo.getA().before(duo.getB());
         }
         throw new IllegalStateException(notComparableMsg(u));
      }
   };
}
 
Example #16
Source File: ClassInstanceIndex.java    From ontopia with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<VariantNameIF> getAllVariantNames() {
  Collection<Collection<VariantNameIF>> collected = CollectionUtils.collect(getAllTopicNames(), 
          new Transformer<TopicNameIF, Collection<VariantNameIF>>() {
    @Override
    public Collection<VariantNameIF> transform(TopicNameIF input) {
      return input.getVariants();
    }
  });
  return createComposite(collected);
}
 
Example #17
Source File: CollectIterableTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test collect.
 */
@Test
public void testCollect(){
    List<String> list = toList("xinge", "feilong1", "feilong2", "feilong2");

    Transformer<String, Object> nullTransformer = TransformerUtils.nullTransformer();
    List<Object> collect = CollectionsUtil.collect(list, nullTransformer);

    Object[] objects = { null, null, null, null };
    assertThat(collect, hasItems(objects));
}
 
Example #18
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** sub. */
public static Transformer<Duo<Number, Number>, Number> sub()
{
   return new Transformer<Duo<Number, Number>, Number>()
   {
      @Override
      public Number transform(Duo<Number, Number> u)
      {
         return u.getA().doubleValue() - u.getB().doubleValue();
      }
   };
}
 
Example #19
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** second. */
public static Transformer<Date, Integer> second()
{
   return new Transformer<Date, Integer>()
   {
      @Override
      public Integer transform(Date u)
      {
         Calendar c = Calendar.getInstance();
         c.setTime(u);
         return c.get(Calendar.SECOND);
      }
   };
}
 
Example #20
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** add. */
public static Transformer<Duo<Number, Number>, Number> add()
{
   return new Transformer<Duo<Number, Number>, Number>()
   {
      @Override
      public Number transform(Duo<Number, Number> u)
      {
         return u.getA().doubleValue() + u.getB().doubleValue();
      }
   };
}
 
Example #21
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** unary minus. */
public static Transformer<Number, Number> minus()
{
   return new Transformer<Number, Number>()
   {
      @Override
      public Number transform(Number u)
      {
         return -(u.doubleValue());
      }
   };
}
 
Example #22
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** not. */
public static Transformer<Boolean, Boolean> not()
{
   return new Transformer<Boolean, Boolean>()
   {
      @Override
      public Boolean transform(Boolean u)
      {
         return !u;
      }
   };
}
 
Example #23
Source File: ToMapTransformerTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test array.
 */
@Test
public void testMap(){
    Map<String, String> map = toMap("1", "2,2");

    Transformer<String, Integer> keyTransformer = new SimpleClassTransformer<>(Integer.class);
    Transformer<String, Integer[]> valueTransformer = new SimpleClassTransformer<>(Integer[].class);

    //key和value转成不同的类型
    Map<Integer, Integer[]> returnMap = toMap(map, keyTransformer, valueTransformer);

    assertThat(returnMap, allOf(hasEntry(1, toArray(2, 2))));
}
 
Example #24
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** and. */
public static Transformer<Duo<Boolean, Boolean>, Boolean> and()
{
   return new Transformer<Duo<Boolean, Boolean>, Boolean>()
   {
      @Override
      public Boolean transform(Duo<Boolean, Boolean> u)
      {
         return u.getA() && u.getB();
      }
   };
}
 
Example #25
Source File: ResourcesDirectoryReader.java    From ontopia with Apache License 2.0 5 votes vote down vote up
public Collection<String> getResourcesAsStrings() {
  if (resources == null) {
    findResources();
  }
  return CollectionUtils.collect(resources, new Transformer<URL, String>() {
    @Override
    public String transform(URL url) {
      try {
        return URLDecoder.decode(url.toString(), "utf-8");
      } catch (UnsupportedEncodingException uee) {
        throw new OntopiaRuntimeException(uee);
      }
    }
  });
}
 
Example #26
Source File: GroupWithTransformerTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test group null transformer.
 */
//*****
@Test(expected = NullPointerException.class)
public void testGroupNullTransformer(){
    List<User> list = toList(new User("张飞", 10), new User("刘备", 10));
    CollectionsUtil.group(list, (Transformer<User, String>) null);
}
 
Example #27
Source File: ExecutableExpressionTree.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a dynamic leave from a Transformer.
 * @param <T> Type of the Collection entry.
 * @param <R> Return type.
 * @param p Transformer that provides us data from the collection entry.
 * @return a new Node.
 */
public static <T,R> Node<T,R> createLeave(final Transformer<T,R> p)
{
   return new Node<T,R>()
   {
      @Override
      public R exec(T element)
      {
         return p.transform(element);
      }
   };
}
 
Example #28
Source File: ToMapTransformerTest.java    From feilong-core with Apache License 2.0 5 votes vote down vote up
/**
 * Test array to array.
 */
@Test
public void testMapToArray(){
    Map<String[], String[]> map = toMap(toArray("1"), toArray("2", "8"));

    Transformer<String[], Integer[]> keyTransformer = new SimpleClassTransformer<>(Integer[].class);
    Transformer<String[], Long[]> valueTransformer = new SimpleClassTransformer<>(Long[].class);

    //key和value转成不同的类型
    Map<Integer[], Long[]> returnMap = toMap(map, keyTransformer, valueTransformer);

    assertThat(returnMap, allOf(hasEntry(toArray(1), toArray(2L, 8L))));
}
 
Example #29
Source File: ExecutableExpressionTree.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a Node from a Transformer<Duo<?,?>,?>.
 * @param <T> Type of the Collection entry.
 * @param <R> Return type.
 * @param t a bi-transformer that accepts a <?,?> and returns a <?>.
 * @param sub1 first  Node at `depth+1` in the tree, serves as  first parameter for `t`.
 * @param sub2 second Node at `depth+1` in the tree. serves as second parameter for `t`.
 * @return a new Node.
 */
public static <T,R> Node<T,R> createNode(final Transformer<Duo<Object,Object>,R> t,
      final Node<T,?> sub1, final Node<T,?> sub2)
{
   return new Node<T,R>()
   {
      @Override
      public R exec(T element)
      {
         Object o1 = sub1.exec(element);
         Object o2 = sub2.exec(element);
         return t.transform(new Duo<>(o1, o2));
      }
   };
}
 
Example #30
Source File: Transformers.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/** mod. */
public static Transformer<Duo<Long, Long>, Long> mod()
{
   return new Transformer<Duo<Long, Long>, Long>()
   {
      @Override
      public Long transform(Duo<Long, Long> u)
      {
         return u.getA() % u.getB();
      }
   };
}