Ejemplo n.º 1
0
 private void init() {
   isOffsetConstant = getOffsetExpression() instanceof LiteralExpression;
   isLengthConstant = getLengthExpression() instanceof LiteralExpression;
   hasLengthExpression =
       !isLengthConstant || ((LiteralExpression) getLengthExpression()).getValue() != null;
   isFixedWidth =
       getStrExpression().getDataType().isFixedWidth()
           && ((hasLengthExpression && isLengthConstant)
               || (!hasLengthExpression && isOffsetConstant));
   if (hasLengthExpression && isLengthConstant) {
     Integer maxLength =
         ((Number) ((LiteralExpression) getLengthExpression()).getValue()).intValue();
     this.byteSize = maxLength >= 0 ? maxLength : 0;
   } else if (isOffsetConstant) {
     Number offsetNumber = (Number) ((LiteralExpression) getOffsetExpression()).getValue();
     if (offsetNumber != null) {
       int offset = offsetNumber.intValue();
       if (getStrExpression().getDataType().isFixedWidth()) {
         if (offset >= 0) {
           byteSize = getStrExpression().getByteSize() - offset - (offset == 0 ? 0 : 1);
         } else {
           byteSize = -offset;
         }
       }
     }
   }
 }
Ejemplo n.º 2
0
  /*
   *  Making sure that the user input is in range
   *  @param1 Scanner object for input
   *  @param2 lowerbound integer
   *  @param3 upperbound integer
   *  @param4 String that specifies for which attribute/field input is for
   *  printing purposes
   *  @param5 ValType enum that specifies whether the primitive value is
   *  an int or double
   *  @param6 boolean that specifies whether the input is an Enum value
   *  @param7 boolean that specifies whether to return the value if out of
   *  range (false) or not (true)
   */
  public static Number makeSureValInRange(
      Scanner input,
      int lowerbound,
      int upperbound,
      String inputFor,
      ValType valType,
      boolean enumeration,
      boolean withinRange) {

    boolean valid = !withinRange;
    Number val = 0;

    while (!valid) {

      System.out.println(inputString(inputFor, null, StringOption.SELECT, enumeration));

      val = valType == ValType.INTEGER ? input.nextInt() : input.nextDouble();

      if (val.intValue() < lowerbound || val.intValue() > upperbound) {
        System.out.println(inputString(inputFor, null, StringOption.INCORRECT, enumeration));
      } else {
        valid = true;
      }
    }
    return val;
  }
Ejemplo n.º 3
0
 /**
  * Tests if this object is equal to another
  *
  * @param a_obj the other object
  * @return true: this object is equal to the other one
  * @author Klaus Meffert
  * @since 2.3
  */
 public boolean equals(final Object a_obj) {
   if (a_obj == null) {
     return false;
   }
   if (a_obj == this) {
     return true;
   }
   if (!(a_obj instanceof KeyedValues)) {
     return false;
   }
   final KeyedValues kvs = (KeyedValues) a_obj;
   final int count = size();
   if (count != kvs.size()) {
     return false;
   }
   for (int i = 0; i < count; i++) {
     final Comparable k1 = getKey(i);
     final Comparable k2 = kvs.getKey(i);
     if (!k1.equals(k2)) {
       return false;
     }
     final Number v1 = getValue(i);
     final Number v2 = kvs.getValue(i);
     if (v1 == null) {
       if (v2 != null) {
         return false;
       }
     } else {
       if (!v1.equals(v2)) {
         return false;
       }
     }
   }
   return true;
 }
Ejemplo n.º 4
0
 @Override
 public long getLong(final String key) {
   final Number number = this.extractNumber(this.findLastTag(key));
   if (number == null) {
     return 0L;
   }
   return number.longValue();
 }
Ejemplo n.º 5
0
 @Override
 public int getInt(final String key) {
   final Number number = this.extractNumber(this.findLastTag(key));
   if (number == null) {
     return 0;
   }
   return number.intValue();
 }
Ejemplo n.º 6
0
 @Override
 public double getDouble(final String key) {
   final Number number = this.extractNumber(this.findLastTag(key));
   if (number == null) {
     return 0.0;
   }
   return number.doubleValue();
 }
Ejemplo n.º 7
0
 /**
  * Looks up the given key in the given map, converting the result into a {@link Short}. First,
  * {@link #getNumber(Map,Object)} is invoked. If the result is null, then null is returned.
  * Otherwise, the short 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 Short} or null
  */
 public static Short getShort(Map map, Object key) {
   Number answer = getNumber(map, key);
   if (answer == null) {
     return null;
   } else if (answer instanceof Short) {
     return (Short) answer;
   }
   return new Short(answer.shortValue());
 }
Ejemplo n.º 8
0
 /**
  * Looks up the given key in the given map, converting the result into a {@link Byte}. First,
  * {@link #getNumber(Map,Object)} is invoked. If the result is null, then null is returned.
  * Otherwise, the byte 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 Byte} or null
  */
 public static Byte getByte(Map map, Object key) {
   Number answer = getNumber(map, key);
   if (answer == null) {
     return null;
   } else if (answer instanceof Byte) {
     return (Byte) answer;
   }
   return new Byte(answer.byteValue());
 }
Ejemplo n.º 9
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());
 }
Ejemplo n.º 10
0
 /**
  * Looks up the given key in the given map, converting the result into a {@link Float}. First,
  * {@link #getNumber(Map,Object)} is invoked. If the result is null, then null is returned.
  * Otherwise, the float 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 Float} or null
  */
 public static Float getFloat(Map map, Object key) {
   Number answer = getNumber(map, key);
   if (answer == null) {
     return null;
   } else if (answer instanceof Float) {
     return (Float) answer;
   }
   return new Float(answer.floatValue());
 }
Ejemplo n.º 11
0
 /**
  * Looks up the given key in the given map, converting the result into a {@link Double}. First,
  * {@link #getNumber(Map,Object)} is invoked. If the result is null, then null is returned.
  * Otherwise, the double 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 Double} or null
  */
 public static Double getDouble(Map map, Object key) {
   Number answer = getNumber(map, key);
   if (answer == null) {
     return null;
   } else if (answer instanceof Double) {
     return (Double) answer;
   }
   return new Double(answer.doubleValue());
 }
Ejemplo n.º 12
0
 /** Return a double parsed from the given string, possibly formatted with a "%" sign */
 public static double parseDouble(String val) throws NumberFormatException, ParseException {
   NumberFormat formatPercent = NumberFormat.getPercentInstance(Locale.US); // for zoom factor
   if (val.indexOf("%") == -1) { // not in percent format !
     return Double.parseDouble(val);
   }
   // else it's a percent format -> parse it
   Number n = formatPercent.parse(val);
   return n.doubleValue();
 }
Ejemplo n.º 13
0
 /**
  * Looks up the given key in the given map, converting the result into an {@link Integer}. First,
  * {@link #getNumber(Map,Object)} is invoked. If the result is null, then null is returned.
  * Otherwise, the integer 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 an {@link Integer} or null
  */
 public static Integer getInteger(Map map, Object key) {
   Number answer = getNumber(map, key);
   if (answer == null) {
     return null;
   } else if (answer instanceof Integer) {
     return (Integer) answer;
   }
   return new Integer(answer.intValue());
 }
Ejemplo n.º 14
0
  /**
   * 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;
  }
Ejemplo n.º 15
0
 public void putByGlobalType(long i, Number element) {
   if (globalType == Type.INT || type == Type.INT) {
     int anElement = element.intValue();
     put(i, anElement);
   } else if (globalType == Type.FLOAT) {
     float anElement = element.floatValue();
     put(i, anElement);
   } else if (globalType == Type.DOUBLE) {
     double anElement = element.doubleValue();
     put(i, anElement);
   }
 }
Ejemplo n.º 16
0
 @Override
 public boolean preservesOrder() {
   if (isOffsetConstant) {
     LiteralExpression literal = (LiteralExpression) getOffsetExpression();
     Number offsetNumber = (Number) literal.getValue();
     if (offsetNumber != null) {
       int offset = offsetNumber.intValue();
       return (offset == 0 || offset == 1) && (!hasLengthExpression || isLengthConstant);
     }
   }
   return false;
 }
Ejemplo n.º 17
0
 /**
  * Looks up the given key in the given map, converting the result into a {@link Boolean}. If the
  * map is null, this method returns null. If the value mapped by the given key is a {@link
  * Boolean}, then it is returned as-is. Otherwise, if the value is a string, then if that string
  * ignoring case equals "true", then a true {@link Boolean} is returned. Any other string value
  * will result in a false {@link Boolean} being returned. OR, if the value is a {@link Number},
  * and that {@link Number} is 0, then a false {@link Boolean} is returned. Any other {@link
  * Number} value results in a true {@link Boolean} being returned.
  *
  * <p>Any value that is not a {@link Boolean}, {@link String} or {@link Number} results in null
  * being returned.
  *
  * <p>
  *
  * @param map the map whose value to look up
  * @param key the key whose value to look up in that map
  * @return a {@link Boolean} or null
  */
 public static Boolean getBoolean(Map map, Object key) {
   if (map != null) {
     Object answer = map.get(key);
     if (answer != null) {
       if (answer instanceof Boolean) {
         return (Boolean) answer;
       } else if (answer instanceof String) {
         return new Boolean((String) answer);
       } else if (answer instanceof Number) {
         Number n = (Number) answer;
         return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE;
       }
     }
   }
   return null;
 }
Ejemplo n.º 18
0
 /**
  * @return a double parsed from the value associated with the given key in the given Properties.
  *     returns "def" in key wasn't found, or if a parsing error occured. If "value" contains a "%"
  *     sign, we use a <code>NumberFormat.getPercentInstance</code> to convert it to a double.
  */
 public static double parseProperty(Properties preferences, String key, double def) {
   NumberFormat formatPercent = NumberFormat.getPercentInstance(Locale.US); // for zoom factor
   String val = preferences.getProperty(key);
   if (val == null) return def;
   if (val.indexOf("%") == -1) { // not in percent format !
     try {
       return Double.parseDouble(val);
     } catch (NumberFormatException nfe) {
       nfe.printStackTrace();
       return def;
     }
   }
   // else it's a percent format -> parse it
   try {
     Number n = formatPercent.parse(val);
     return n.doubleValue();
   } catch (ParseException ex) {
     ex.printStackTrace();
     return def;
   }
 }
Ejemplo n.º 19
0
  public boolean getBoolean() throws SQLException {
    int type = JSONTypes.jsonTypes.get(field.getType());

    switch (type) {
      case JSONTypes.JSON_BOOLEAN:
        return (boolean) jsonObject;
      case JSONTypes.JSON_NUMBER:
        Number number = (Number) jsonObject;
        return !number.equals((Number) 0);
      case JSONTypes.JSON_STRING:
        String string = (String) jsonObject;
        return !string.isEmpty();
      case JSONTypes.JSON_MAP:
      case JSONTypes.JSON_OBJECT:
        Map map = (Map) jsonObject;
        return !map.isEmpty();
      case JSONTypes.JSON_ARRAY:
        List list = (List) jsonObject;
        return !list.isEmpty();

      default:
        return false;
    }
  }
Ejemplo n.º 20
0
 /** Calls replReturnedResult() */
 public Void forNumberValue(Number n) {
   _interactionsModel.replReturnedResult(n.toString(), InteractionsDocument.NUMBER_RETURN_STYLE);
   return null;
 }
Ejemplo n.º 21
0
 @Override
 public boolean matches(Number value) {
   return value.intValue() >= minimum;
 }
Ejemplo n.º 22
0
 @Override
 public boolean getBoolean(final String key) {
   final Number number = this.extractNumber(this.findLastTag(key));
   return number != null && number.byteValue() >= 1;
 }
Ejemplo n.º 23
0
 @Override
 public void assign(Number value, long offset) {
   // note here that the final put will take care of the offset
   for (long i = offset; i < length(); i++) put(i, value.doubleValue());
 }
Ejemplo n.º 24
0
 private void writeNumberProperty(Writer writer, Number integer) throws IOException {
   writer.write(integer.toString());
   writer.write(NEW_LINE);
 }
Ejemplo n.º 25
0
 // Subtract given number from given key
 public void sub(String key, Number n) {
   BigDecimal v =
       new BigDecimal(properties.get(key).toString()).add(new BigDecimal("" + n.toString()));
   properties.put(key, v.toString());
 }
 public double getStrokeMiterLimitFactor() {
   Number value = (Number) getAttribute(AttributeKeys.STROKE_MITER_LIMIT);
   return (value != null) ? value.doubleValue() : 10f;
 }
Ejemplo n.º 27
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()
            + ")");
  }
Ejemplo n.º 28
0
 public void setUpperCtrlLimit(Number limit) {
   _ucl = new Short(limit.shortValue());
 }
Ejemplo n.º 29
0
 public void setLowerCtrlLimit(Number limit) {
   _lcl = new Short(limit.shortValue());
 }
Ejemplo n.º 30
0
 @Override
 public void assign(Number value, int offset) {
   for (int i = offset; i < length(); i++) put(i, value.doubleValue());
 }