Package jfun.yan

Examples of jfun.yan.Container


    public String getName() {
      return name;
    }
  }
  public void testHashmapComponent(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.static_method(Avenue.class, "instance");
    yan.registerComponent("target", Components.hashmap(
        new String[]{"first", "second", "third"},
        new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
            newAvenue.withArgument(0, Components.value("Lincoln")),
            newAvenue.withArgument(0, Components.value("Fullerton"))}
        )
    );
    assertEquals(java.util.LinkedHashMap.class, yan.verifyType(HashMap.class));
    final HashMap hmap = (HashMap)yan.getInstanceOfType(java.util.Map.class);
    verifyLinkedHashMap((LinkedHashMap)hmap);
    final Component bad_hash = Components.hashmap(
        new String[]{"a","b"},
        new Creator[]{
            Components.useKey("a").withState("hello"),
            Components.useState(new Predicate(){
              public boolean isObject(Object obj){
                return false;
              }
            })
        }
    );
    yan.registerComponent("bad_hash", bad_hash);
    try{
      yan.getInstance("bad_hash");
      fail("should have failed with UnresolvedComponentException");
    }
    catch(jfun.yan.UnresolvedComponentException e){
      assertTraceSize(e,3);
      assertEntry(e, 0, "useKey <a>");
View Full Code Here


      assertEntry(e, 1, bad_hash);
      assertEntry(e, 2, "getInstance <bad_hash>");
    }
  }
  public void testHashMapComponentWithLessKey(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.static_method(Avenue.class, "instance");
    try{
      yan.registerComponent("target", Components.hashmap(
          new String[]{"first", "second"},
          new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
              newAvenue.withArgument(0, Components.value("Lincoln")),
              newAvenue.withArgument(0, Components.value("Fullerton"))}
          )
View Full Code Here

    }
  }
  public void testHashMapComponentWithDuplicateKey(){
    //in the new design, we do not check key dups.
    //wrappers such as Nuts check duplicates.
    final Container yan = createYanContainer();
    final Component newAvenue = Components.static_method(Avenue.class, "instance");
    //try{
      yan.registerComponent("target", Components.hashmap(
          new String[]{"first", "second", "second"},
          new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
              newAvenue.withArgument(0, Components.value("Lincoln")),
              newAvenue.withArgument(0, Components.value("Fullerton"))}
          )
View Full Code Here

    //catch(IllegalArgumentException e){
    //  assertEquals("duplicate key: second", e.getMessage());
    //}
  }
  public void testCreateLinkedHashMapUsingMap(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.useKey("factory");
    final Component factory = Components.static_method(Avenue.class, "instance")
    .incomplete();
    final String[] keys = new String[]{"first", "second", "third"};
    final Component vals = Components.array(new Component[]{newAvenue.withArgument(0, Components.value("Randolph")),
        newAvenue.withArgument(0, Components.value("Lincoln")),
        newAvenue.withArgument(0, Components.value("Fullerton"))});
    final Component cc = vals.map(new jfun.yan.Map(){
      public Object map(Object obj){
        final Object[] vs = (Object[])obj;
        final LinkedHashMap lhm = new LinkedHashMap(vs.length);
        for(int i=0; i<vs.length; i++){
          lhm.put(keys[i], vs[i]);
        }
        return lhm;
      }
    }).cast(LinkedHashMap.class);
    yan.registerComponent(cc);
    yan.registerComponent("factory", factory);
    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);

  }
View Full Code Here

    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);

  }
  public void testCreateLinkedHashMapUsingMap2(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.useKey("factory");
    final Component factory = Components.static_method(Avenue.class, "instance")
    .incomplete();
    final Component keys = Components.value(new String[]{"first", "second", "third"});
    final Component vals = Components.list(new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
        newAvenue.withArgument(0, Components.value("Lincoln")),
        newAvenue.withArgument(0, Components.value("Fullerton"))});
    final Component cc = Monad.map(keys, vals, new Map2(){
      public Object map(Object o1, Object o2){
        final Object[] ks = (Object[])o1;
        final java.util.List vs = (java.util.List)o2;
        final LinkedHashMap lhm = new LinkedHashMap(ks.length);
        for(int i=0;i<vs.size();i++){
          lhm.put(ks[i], vs.get(i));
        }
        return lhm;
      }
    }).cast(LinkedHashMap.class);
    yan.registerComponent(cc);
    yan.registerComponent("factory", factory);
    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);
  }
View Full Code Here

    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);
  }
  public void testCreateLinkedHashMapUsingBind(){
    final Container yan = createYanContainer();
    final Component newAvenue = Components.useKey("factory");
    final Component factory = Components.static_method(Avenue.class, "instance")
    .incomplete();
    //final Component keys = Components.value(new String[]{"first", "second", "third"});
    final Component vals = Components.list(new Creator[]{newAvenue.withArgument(0, Components.value("Randolph")),
        newAvenue.withArgument(0, Components.value("Lincoln")),
        newAvenue.withArgument(0, Components.value("Fullerton"))});
    final Component cc = Monad.bind(vals, new jfun.yan.Binder(){
      public Creator bind(Object obj){
        final Object[] ks = new String[]{"first", "second", "third"};
        final java.util.List vs = (java.util.List)obj;
        final LinkedHashMap lhm = new LinkedHashMap();
        for(int i=0; i<ks.length; i++){
          lhm.put(ks[i], vs.get(i));
        }
        return Components.value(lhm);
      }
    }).cast(LinkedHashMap.class);
    yan.registerComponent(cc);
    yan.registerComponent("factory", factory);
    yan.verify();
    final LinkedHashMap lhm = (LinkedHashMap)yan.getInstance(LinkedHashMap.class);
    verifyLinkedHashMap(lhm);
  }
View Full Code Here

    a = (Avenue)lhm.get(k);
    assertEquals(from+2, a.getId());
    assertEquals("Fullerton", a.getName());
  }
  public void testArrayComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent("target",
        Components.array(
        new Component[]{Components.value("hello"), Components.value("world")}));
    assertEquals(String[].class, yan.verifyComponent(
        yan.getComponent("target")));
    String[] arr = (String[])yan.getInstance("target");
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
View Full Code Here

    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
  public void testArrayComponentWithDynamicBoundComponent(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.array(
        new Component[]{Components.value("hello"), Components.useKey("you")}));
    yan.registerValue("you", "world");
    assertEquals(Object[].class, yan.verifyComponent(yan.getComponent("target")));
    Object[] arr = (Object[])yan.getInstanceOfType(Object[].class);
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
View Full Code Here

    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
  public void testArrayComponentWithComponentTypeSpecified(){
    final Container yan = createYanContainer();
    yan.registerComponent(
        Components.array(
        new Component[]{Components.value("hello"), Components.useKey("you")}
        , CharSequence.class));
    yan.registerValue("you", "world");
    assertEquals(CharSequence[].class, yan.verifyComponent(yan.getComponent(CharSequence[].class)));
    CharSequence[] arr = (CharSequence[])yan.getInstanceOfType(CharSequence[].class);
    assertFalse(arr instanceof String[]);
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
View Full Code Here

    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals("world", arr[1]);
  }
  public void testArrayComponentWithRaggedType(){
    final Container yan = createYanContainer();
    yan.registerComponent("target", Components.array(
        new Component[]{Components.value("hello"),
            Components.useKey("age")}));
    yan.registerValue("age", new Integer(20));
    assertEquals(Object[].class, yan.verifyComponent(yan.getComponent("target")));
    Object[] arr = (Object[])yan.getInstanceOfType(Object[].class);
   
    assertEquals(2, arr.length);
    assertEquals("hello", arr[0]);
    assertEquals(new Integer(20), arr[1]);
  }
View Full Code Here

TOP

Related Classes of jfun.yan.Container

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.