org.nutz.lang.Lang.each()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(205)

本文整理了Java中org.nutz.lang.Lang.each()方法的一些代码示例,展示了Lang.each()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Lang.each()方法的具体详情如下:
包路径:org.nutz.lang.Lang
类名称:Lang
方法名:each

Lang.each介绍

[英]用回调的方式,遍历一个对象,可以支持遍历

  • 数组
  • 集合
  • Map
  • 单一元素
    [中]用回调的方式,遍历一个对象,可以支持遍历
  • 数组
  • 集合
    *地图
  • 单一元素

代码示例

代码示例来源:origin: nutzam/nutz

public <T> T fetchLinks(final T obj, final String regex, final Condition cnd) {
  if (null == obj)
    return null;
  Lang.each(obj, false, new Each<Object>() {
    public void invoke(int index, Object ele, int length) {
      _fetchLinks(ele, regex, true, true, true, cnd);
    }
  });
  return obj;
}

代码示例来源:origin: nutzam/nutz

public <T> T clearLinks(T obj, final String regex) {
  if (null == obj)
    return null;
  Lang.each(obj, false, new Each<Object>() {
    public void invoke(int index, Object ele, int length) {
      EntityOperator opt = _optBy(ele);
      if (null == opt)
        return;
      opt.entity.visitMany(ele, regex, doClear(opt));
      opt.entity.visitManyMany(ele, regex, doClearRelationByHostField(opt));
      opt.entity.visitOne(ele, regex, doClear(opt));
      opt.exec();
    }
  });
  return obj;
}

代码示例来源:origin: nutzam/nutz

public <T> T updateLinks(T obj, final String regex) {
  if (null == obj)
    return null;
  Lang.each(obj, false, new Each<Object>() {
    public void invoke(int index, Object ele, int length) throws ExitLoop, ContinueLoop,
        LoopException {
      EntityOperator opt = _optBy(ele);
      if (null == opt)
        return;
      opt.entity.visitOne(ele, regex, doUpdate(opt));
      opt.entity.visitMany(ele, regex, doUpdate(opt));
      opt.entity.visitManyMany(ele, regex, doUpdate(opt));
      opt.exec();
    }
  });
  return obj;
}

代码示例来源:origin: nutzam/nutz

/**
 * 用回调的方式,遍历一个对象,可以支持遍历
 * <ul>
 * <li>数组
 * <li>集合
 * <li>Map
 * <li>单一元素
 * </ul>
 *
 * @param obj
 *            对象
 * @param callback
 *            回调
 */
public static <T> void each(Object obj, Each<T> callback) {
  each(obj, true, callback);
}

代码示例来源:origin: nutzam/nutz

public <T> T updateWith(T obj, final String regex) {
  if (null == obj)
    return null;
  Lang.each(obj, false, new Each<Object>() {
    public void invoke(int index, Object ele, int length) throws ExitLoop, ContinueLoop,
        LoopException {
      EntityOperator opt = _optBy(ele);
      if (null == opt)
        return;
      opt.entity.visitOne(ele, regex, doUpdate(opt));
      opt.addUpdate();
      opt.entity.visitMany(ele, regex, doUpdate(opt));
      opt.entity.visitManyMany(ele, regex, doUpdate(opt));
      opt.exec();
    }
  });
  return obj;
}

代码示例来源:origin: nutzam/nutz

public int deleteLinks(Object obj, final String regex) {
  if (null == obj)
    return 0;
  final int[] re = new int[1];
  Lang.each(obj, false, new Each<Object>() {
    public void invoke(int index, Object ele, int length) throws ExitLoop, ContinueLoop,
        LoopException {
      EntityOperator opt = _optBy(ele);
      if (null == opt)
        return;
      opt.entity.visitMany(ele, regex, doDelete(opt));
      opt.entity.visitManyMany(ele, regex, doClearRelationByLinkedField(opt));
      opt.entity.visitManyMany(ele, regex, doDelete(opt));
      opt.entity.visitOne(ele, regex, doDelete(opt));
      re[0] += opt.exec().getUpdateCount();
    }
  });
  return re[0];
}

代码示例来源:origin: nutzam/nutz

public int deleteWith(Object obj, final String regex) {
  if (null == obj)
    return 0;
  final int[] re = new int[1];
  Lang.each(obj, false, new Each<Object>() {
    public void invoke(int index, Object ele, int length) throws ExitLoop, ContinueLoop,
        LoopException {
      EntityOperator opt = _optBy(ele);
      if (null == opt)
        return;
      opt.entity.visitMany(ele, regex, doDelete(opt));
      opt.entity.visitManyMany(ele, regex, doClearRelationByLinkedField(opt));
      opt.entity.visitManyMany(ele, regex, doDelete(opt));
      opt.addDeleteSelfOnly();
      opt.entity.visitOne(ele, regex, doDelete(opt));
      re[0] += opt.exec().getUpdateCount();
    }
  });
  return re[0];
}

代码示例来源:origin: nutzam/nutz

public void updateLinkedField(Object obj, Object linked) {
  if (null != hostField) {
    final Object v = hostField.getValue(obj);
    Lang.each(linked, new Each<Object>() {
      public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
        linkedField.setValue(ele, v);
      }
    });
  }
}

代码示例来源:origin: nutzam/nutz

public String getURLEncodedParams() {
  final StringBuilder sb = new StringBuilder();
  if (params != null) {
    for (Entry<String, Object> en : params.entrySet()) {
      final String key = en.getKey();
      Object val = en.getValue();
      if (val == null)
        val = "";
      Lang.each(val, new Each<Object>() {
        public void invoke(int index, Object ele, int length)
            throws ExitLoop, ContinueLoop, LoopException {
          if (offEncode) {
            sb.append(key).append('=').append(ele).append('&');
          } else {
            sb.append(Http.encode(key, enc))
             .append('=')
             .append(Http.encode(ele, enc))
             .append('&');
          }
        }
      });
    }
    if (sb.length() > 0)
      sb.setLength(sb.length() - 1);
  }
  return sb.toString();
}

代码示例来源:origin: nutzam/nutz

@Override
  public int getEstimationSize() throws IOException {
    final int[] count = new int[1];
    for (Entry<String, ?> entry : request.getParams().entrySet()) {
      count[0] += 60;
      final String key = entry.getKey();
      Object val = entry.getValue();
      if (val == null)
        val = "";
      Lang.each(val, new Each<Object>() {
        public void invoke(int index, Object ele, int length){
          if (ele instanceof File)
            count[0]+= ((File)ele).length() + 100;
          else
            try {
              count[0] += (key+ele).getBytes(request.getEnc()).length + 100;
            }
            catch (UnsupportedEncodingException e) {
            }
        }
      });
    }
    return count[0];
  }
}

代码示例来源:origin: nutzam/nutz

@SuppressWarnings("unchecked")
public Object get(ServletContext sc, HttpServletRequest req, HttpServletResponse resp, Object refer) {
  if (refer == null)
    return null;
  Object obj = ((Map<String, Object>) refer).get(name);
  if (obj == null || Lang.eleSize(obj) == 0)
    return EMTRY;
  if (Lang.eleSize(obj) == 1) {
    Object tmp = Lang.first(obj);
    if (tmp == null || !(tmp instanceof TempFile))
      return EMTRY;
    return new TempFile[]{(TempFile)tmp};
  }
  final List<TempFile> list = new ArrayList<TempFile>();
  Lang.each(obj, new Each<Object>() {
    public void invoke(int index, Object ele, int length) throws ExitLoop, ContinueLoop, LoopException {
      if (ele instanceof TempFile) {
        list.add((TempFile)ele);
      }
    }
  });
  if (list.isEmpty())
    return EMTRY;
  return list.toArray(new TempFile[list.size()]);
}

代码示例来源:origin: nutzam/nutz

public int joinParams(Entity<?> en, Object obj, final Object[] params, final int off) {
  VarSet row = (VarSet) obj;
  Object val = row.get(name);
  if (val == null) {
    return off + 1;
  } else if (val instanceof PItem) {
    return ((PItem) val).joinParams(en, null, params, off);
  } else if (val.getClass().isArray()) {
    int len = Lang.eleSize(val);
    Lang.each(val, new Each<Object>() {
      public void invoke(int index, Object ele, int length) {
        params[off + index] = ele;
      }
    });
    return off + len;
    // } else if (val instanceof Condition) {
  } else {
    params[off] = val;
    return off + 1;
  }
}

代码示例来源:origin: nutzam/nutz

public List<Pojo> addUpdateForIgnoreNull(    final Entity<?> en,
                      final Object obj,
                      final FieldMatcher fm) {
  if (null == en)
    return null;
  final FieldMatcher newFM;
  if (null == fm)
    newFM = FieldMatcher.make(null, null, true);
  else {
    newFM = fm;
    newFM.setIgnoreNull(true);
  }
  final List<Pojo> re = new ArrayList<Pojo>(Lang.eleSize(obj));
  Lang.each(obj, new Each<Object>() {
    public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
      Pojo pojo = dao.pojoMaker.makeUpdate(en, ele)
                    .append(Pojos.Items.cndAuto(en, ele))
                    .setOperatingObject(ele);
      pojo.getContext().setFieldMatcher(newFM);
      re.add(pojo);
    }
  });
  pojoList.addAll(re);
  return re;
}

代码示例来源:origin: nutzam/nutz

public Object invoke(Connection conn, ResultSet rs, final Pojo pojo, Statement stmt)
      throws SQLException {
    final ResultSet _rs = stmt.getGeneratedKeys();
    Object obj = pojo.getOperatingObject();
    if (obj instanceof Map) {
      obj = Arrays.asList(obj);
    }
    Lang.each(obj, new Each<Object>() {
      public void invoke(int index, Object ele, int length)
          throws ExitLoop, ContinueLoop, LoopException {
        try {
          if (!_rs.next())
            throw new ExitLoop();
          Object key = _rs.getObject(1);
          pojo.getEntity().getIdField().setValue(ele, key);
        }
        catch (SQLException e) {
          throw new DaoException(e);
        }
      }
    });
    return pojo.getOperatingObject();
  }
}

代码示例来源:origin: nutzam/nutz

@Override
@SuppressWarnings("unchecked")
public <T> List<T> getList(String key, final Class<T> eleType, List<T> dft) {
  Object v = get(key);
  if (null == v)
    return dft;
  if (v instanceof CharSequence) {
    return Lang.list(Castors.me().castTo(v, eleType));
  }
  int len = Lang.eleSize(v);
  final List<T> list = new ArrayList<T>(len);
  Lang.each(v, new Each<Object>() {
    @Override
    public void invoke(int index, Object ele, int length) {
      list.add(Castors.me().castTo(ele, eleType));
    }
  });
  return list;
}

代码示例来源:origin: nutzam/nutz

@Override
@SuppressWarnings("unchecked")
public <T> T[] getArray(String key, final Class<T> eleType, T[] dft) {
  Object v = get(key);
  if (null == v)
    return dft;
  if (v instanceof CharSequence) {
    return Lang.array(Castors.me().castTo(v, eleType));
  }
  int len = Lang.eleSize(v);
  final Object arr = Array.newInstance(eleType, len);
  final int[] i = new int[]{0};
  Lang.each(v, new Each<Object>() {
    @Override
    public void invoke(int index, Object ele, int length) {
      Array.set(arr, i[0]++, Castors.me().castTo(ele, eleType));
    }
  });
  return (T[]) arr;
}

代码示例来源:origin: nutzam/nutz

public void visit(final Object obj, LinkField lnk) {
  // 只有多对多的映射才被考虑
  if (lnk instanceof ManyManyLinkField) {
    // 获取两边映射主键的值
    final ManyManyLinkField mm = (ManyManyLinkField) lnk;
    Object value = lnk.getValue(obj);
    
    final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(Lang.eleSize(value));
    Lang.each(value, new Each<Object>() {
      public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
        list.add(new RelationObjectMap(mm, obj, ele));
      }
    });
    if (list.isEmpty())
      return;
    Entity<Map<String, Object>> en = holder.makeEntity(mm.getRelationName(), list.get(0));
    Pojo pojo = opt.maker().makeInsert(en);
    pojo.setOperatingObject(list);
    for (Object p : list)
      pojo.addParamsBy(p);
    opt.add(pojo);
  }
}

代码示例来源:origin: nutzam/nutz

public <T> T insert(final T obj) {
  Object first = Lang.first(obj);
  final EntityOperator opt = _optBy(first);
  if (null == opt)
    return null;
  int size = Lang.eleSize(obj);
  opt.addInsert(opt.entity, first);
  if (size > 1) {
    if (opt.getPojoListSize() == 1) {
      // 单一操作,可以转为批量插入
      return fastInsert(obj);
    }
    Lang.each(obj, false, new Each<Object>() {
      public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
        if (i != 0)
          opt.addInsert(opt.entity, ele);
      }
    });
  }
  opt.exec();
  return obj;
}

代码示例来源:origin: nutzam/nutz

public void visit(Object obj, LinkField lnk) {
  if (lnk instanceof ManyManyLinkField) {
    final ManyManyLinkField mm = (ManyManyLinkField) lnk;
    Object value = mm.getValue(obj);
    if (Lang.eleSize(value) == 0)
      return;
    final Pojo pojo = opt.maker().makeDelete(mm.getRelationName());
    pojo.append(Pojos.Items.cndColumn(mm.getToColumnName(), mm.getLinkedField(), null));
    Lang.each(value, new Each<Object>() {
      public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
        pojo.addParamsBy(mm.getLinkedField().getValue(ele));
      }
    });
    opt.add(pojo);
  }
}

代码示例来源:origin: nutzam/nutz

public void visit(Object obj, LinkField lnk) {
  Object value = lnk.getValue(obj);
  if (value == null || Lang.eleSize(value) == 0) {
    log.infof("Value of LinkField(@%s-->%s.%s) is null or isEmtry, ingore",
          lnk.getLinkType(), lnk.getEntity().getType().getSimpleName(),
          lnk.getHostField().getName());
    return;
  }
  final Pojo pojo = opt.maker().makeDelete(lnk.getLinkedEntity());
  pojo.setOperatingObject(value);
  pojo.append(Pojos.Items.cndAuto(lnk.getLinkedEntity(), null));
  Lang.each(value, new Each<Object>() {
    public void invoke(int i, Object ele, int length) throws ExitLoop, LoopException {
      pojo.addParamsBy(ele);
    }
  });
  opt.add(pojo);
}

相关文章