コード例 #1
0
ファイル: NBTStorage.java プロジェクト: TCPR/Citizens2
 @Override
 public long getLong(final String key) {
   final Number number = this.extractNumber(this.findLastTag(key));
   if (number == null) {
     return 0L;
   }
   return number.longValue();
 }
コード例 #2
0
 /**
  * Looks up the given key in the given map, converting the result into a {@link Long}. First,
  * {@link #getNumber(Map,Object)} is invoked. If the result is null, then null is returned.
  * Otherwise, the long value of the resulting {@link Number} is returned.
  *
  * @param map the map whose value to look up
  * @param key the key whose value to look up in that map
  * @return a {@link Long} or null
  */
 public static Long getLong(Map map, Object key) {
   Number answer = getNumber(map, key);
   if (answer == null) {
     return null;
   } else if (answer instanceof Long) {
     return (Long) answer;
   }
   return new Long(answer.longValue());
 }
コード例 #3
0
ファイル: DB2PlugIn.java プロジェクト: BhavinGitHub/wonder
  /**
   * Overrides the parent implementation to provide a more efficient mechanism for generating
   * primary keys, while generating the primary key support on the fly.
   *
   * @param count the batch size
   * @param entity the entity requesting primary keys
   * @param channel open JDBCChannel
   * @return NSArray of NSDictionary where each dictionary corresponds to a unique primary key value
   */
  public NSArray newPrimaryKeys(int count, EOEntity entity, JDBCChannel channel) {
    if (isPrimaryKeyGenerationNotSupported(entity)) {
      return null;
    }

    EOAttribute attribute = (EOAttribute) entity.primaryKeyAttributes().lastObject();
    String attrName = attribute.name();
    boolean isIntType = "i".equals(attribute.valueType());

    NSMutableArray results = new NSMutableArray(count);
    String sequenceName = sequenceNameForEntity(entity);
    DB2Expression expression = new DB2Expression(entity);

    boolean succeeded = false;
    for (int tries = 0; !succeeded && tries < 2; tries++) {
      while (results.count() < count) {
        try {
          StringBuffer sql = new StringBuffer();
          sql.append("SELECT ");
          sql.append("next value for " + sequenceName + " AS KEY");
          sql.append(" from sysibm.sysdummy1");
          expression.setStatement(sql.toString());
          channel.evaluateExpression(expression);
          try {
            NSDictionary row;
            while ((row = channel.fetchRow()) != null) {
              Enumeration pksEnum = row.allValues().objectEnumerator();
              while (pksEnum.hasMoreElements()) {
                Number pkObj = (Number) pksEnum.nextElement();
                Number pk;
                if (isIntType) {
                  pk = Integer.valueOf(pkObj.intValue());
                } else {
                  pk = Long.valueOf(pkObj.longValue());
                }
                results.addObject(new NSDictionary(pk, attrName));
              }
            }
          } finally {
            channel.cancelFetch();
          }
          succeeded = true;
        } catch (JDBCAdaptorException ex) {
          throw ex;
        }
      }
    }

    if (results.count() != count) {
      throw new IllegalStateException(
          "Unable to generate primary keys from the sequence for " + entity + ".");
    }

    return results;
  }
コード例 #4
0
  /**
   * Helper method to try to call appropriate write method for given untyped Object. At this point,
   * no structural conversions should be done, only simple basic types are to be coerced as
   * necessary.
   *
   * @param value Non-null value to write
   */
  protected void _writeSimpleObject(Object value) throws IOException, JsonGenerationException {
    /* 31-Dec-2009, tatu: Actually, we could just handle some basic
     *    types even without codec. This can improve interoperability,
     *    and specifically help with TokenBuffer.
     */
    if (value == null) {
      writeNull();
      return;
    }
    if (value instanceof String) {
      writeString((String) value);
      return;
    }
    if (value instanceof Number) {
      Number n = (Number) value;
      if (n instanceof Integer) {
        writeNumber(n.intValue());
        return;
      } else if (n instanceof Long) {
        writeNumber(n.longValue());
        return;
      } else if (n instanceof Double) {
        writeNumber(n.doubleValue());
        return;
      } else if (n instanceof Float) {
        writeNumber(n.floatValue());
        return;
      } else if (n instanceof Short) {
        writeNumber(n.shortValue());
        return;
      } else if (n instanceof Byte) {
        writeNumber(n.byteValue());
        return;
      } else if (n instanceof BigInteger) {
        writeNumber((BigInteger) n);
        return;
      } else if (n instanceof BigDecimal) {
        writeNumber((BigDecimal) n);
        return;

        // then Atomic types

      } else if (n instanceof AtomicInteger) {
        writeNumber(((AtomicInteger) n).get());
        return;
      } else if (n instanceof AtomicLong) {
        writeNumber(((AtomicLong) n).get());
        return;
      }
    } else if (value instanceof byte[]) {
      writeBinary((byte[]) value);
      return;
    } else if (value instanceof Boolean) {
      writeBoolean(((Boolean) value).booleanValue());
      return;
    } else if (value instanceof AtomicBoolean) {
      writeBoolean(((AtomicBoolean) value).get());
      return;
    }
    throw new IllegalStateException(
        "No ObjectCodec defined for the generator, can only serialize simple wrapper types (type passed "
            + value.getClass().getName()
            + ")");
  }