arrayvalue.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,663 行 · 第 1/3 页

JAVA
1,663
字号
  abstract public Var getRef(Value index);  /**   * Returns an iterator of the entries.   */  public Set<Value> keySet()  {    return new KeySet();  }  /**   * Returns a set of all the of the entries.   */  public Set<Map.Entry<Value,Value>> entrySet()  {    return new EntrySet();  }  /**   * Returns a collection of the values.   */  public Collection<Value> values()  {    return new ValueCollection();  }    /**   * Convenience for lib.   */  public void put(String key, String value)  {    // XXX: this needs an Env arg because of i18n    // XXX: but some  modules have arrays that are static constants    put(StringValue.create(key), StringValue.create(value));  }  /**   * Convenience for lib.   */  public void put(Env env, String key, String value)  {    put(env.createString(key), env.createString(value));  }  /**   * Convenience for lib.   */  public void put(String key, char value)  {    // XXX: this needs an Env arg because of i18n    put(StringValue.create(key), StringValue.create(value));  }  /**   * Convenience for lib.   */  public void put(String key, long value)  {    // XXX: this needs an Env arg because of i18n    put(StringValue.create(key), new LongValue(value));  }  /**   * Convenience for lib.   */  public void put(Env env, String key, long value)  {    put(env.createString(key), new LongValue(value));  }    /**   * Convenience for lib.   */  public void put(String key, double value)  {    // XXX: this needs an Env arg because of i18n    put(StringValue.create(key), new DoubleValue(value));  }  /**   * Convenience for lib.   */  public void put(String key, boolean value)  {    // XXX: this needs an Env arg because of i18n    put(StringValue.create(key), value ? BooleanValue.TRUE : BooleanValue.FALSE);  }  /**   * Convenience for lib.   */  public void put(Env env, String key, boolean value)  {    put(env.createString(key),	value ? BooleanValue.TRUE : BooleanValue.FALSE);  }  /**   * Convenience for lib.   */  public void put(String value)  {    // XXX: this needs an Env arg because of i18n    put(StringValue.create(value));  }  /**   * Convenience for lib.   */  public void put(long value)  {    put(new LongValue(value));  }  /**   * Appends as an argument - only called from compiled code   *   * XXX: change name to appendArg   */  public ArrayValue append(Value key, Value value)  {    put(key, value);    return this;  }  /**   * Appends as an argument - only called from compiled code   *   * XXX: change name to appendArg   */  public ArrayValue append(Value value)  {    put(value);    return this;  }  /**   * Puts all of the arg elements into this array.   */  public void putAll(ArrayValue array)  {    for (Map.Entry<Value, Value> entry : array.entrySet())      put(entry.getKey(), entry.getValue());  }  /**   * Convert to an array.   */  public static Value toArray(Value value)  {    value = value.toValue();    if (value instanceof ArrayValue)      return value;    else      return new ArrayValueImpl().put(value);  }  /**   * Prints the value.   * @param env   */  @Override  public void print(Env env)  {    env.print("Array");  }  /**   * Pops the top value.   */  abstract public Value pop();  /**   * Shuffles the array   */  abstract public void shuffle();  /**   * Returns the head.   */  abstract public Entry getHead();  /**   * Returns the tail.   */  abstract protected Entry getTail();  /**   * Returns the current value.   */  @Override  public Value current()  {    if (_current != null)      return _current.getValue();    else      return BooleanValue.FALSE;  }  /**   * Returns the current key   */  @Override  public Value key()  {    if (_current != null)      return _current.getKey();    else      return NullValue.NULL;  }  /**   * Returns true if there are more elements.   */  @Override  public boolean hasCurrent()  {    return _current != null;  }  /**   * Returns the next value.   */  @Override  public Value next()  {    if (_current != null)      _current = _current._next;    return current();  }  /**   * Returns the previous value.   */  public Value prev()  {    if (_current != null)      _current = _current._prev;    return current();  }  /**   * The each iterator   */  public Value each()  {    if (_current == null)      return NullValue.NULL;    ArrayValue result = new ArrayValueImpl();    result.put(LongValue.ZERO, _current.getKey());    result.put(KEY, _current.getKey());    result.put(LongValue.ONE, _current.getValue());    result.put(VALUE, _current.getValue());    _current = _current._next;    return result;  }  /**   * Returns the first value.   */  public Value reset()  {    _current = getHead();    return current();  }  /**   * Returns the last value.   */  public Value end()  {    _current = getTail();    return current();  }  /**   * Returns the corresponding key if this array contains the given value   *   * @param value  the value to search for in the array   *   * @return the key if it is found in the array, NULL otherwise   *   * @throws NullPointerException   */  public Value contains(Value value)  {    for (Entry entry = getHead(); entry != null; entry = entry._next) {      if (entry.getValue().eq(value))        return entry.getKey();    }    return NullValue.NULL;  }  /**   * Returns the corresponding key if this array contains the given value   *   * @param value  the value to search for in the array   *   * @return the key if it is found in the array, NULL otherwise   *   * @throws NullPointerException   */  public Value containsStrict(Value value)  {    for (Entry entry = getHead(); entry != null; entry = entry._next) {      if (entry.getValue().eql(value))        return entry.getKey();    }    return NullValue.NULL;  }  /**   * Returns the corresponding valeu if this array contains the given key   *   * @param key  the key to search for in the array   *   * @return the value if it is found in the array, NULL otherwise   *   * @throws NullPointerException   */  abstract public Value containsKey(Value key);  /**   * Returns an object array of this array.  This is a copy of this object's   * backing structure.  Null elements are not included.   *   * @return an object array of this array   */  public Map.Entry<Value, Value>[] toEntryArray()  {    ArrayList<Map.Entry<Value, Value>> array =      new ArrayList<Map.Entry<Value, Value>>(getSize());    for (Entry entry = getHead(); entry != null; entry = entry._next)      array.add(entry);    Map.Entry<Value, Value>[]result = new Entry[array.size()];    return array.toArray(result);  }  /**   * Sorts this array based using the passed Comparator   *   * @param comparator the comparator for sorting the array   * @param resetKeys  true if the keys should not be preserved   * @param strict  true if alphabetic keys should not be preserved   */  public void sort(Comparator<Map.Entry<Value, Value>> comparator,                   boolean resetKeys, boolean strict)  {    Entry []entries;    entries = new Entry[getSize()];    int i = 0;    for (Entry entry = getHead(); entry != null; entry = entry._next) {      entries[i++] = entry;    }    Arrays.sort(entries, comparator);    clear();    long base = 0;    if (! resetKeys)      strict = false;    for (int j = 0; j < entries.length; j++) {      Value key = entries[j].getKey();      if (resetKeys && (! (key instanceof StringValue) || strict))        put(LongValue.create(base++), entries[j].getValue());      else        put(entries[j].getKey(), entries[j].getValue());    }  }  /*   * Serializes the value.   *    * @param sb holds result of serialization   * @param serializeMap holds reference indexes   */  @Override  public void serialize(StringBuilder sb, SerializeMap serializeMap)  {    sb.append("a:");    sb.append(getSize());    sb.append(":{");        serializeMap.incrementIndex();    for (Entry entry = getHead(); entry != null; entry = entry._next) {      entry.getKey().serialize(sb);      entry.getRawValue().serialize(sb, serializeMap);    }    sb.append("}");  }  /**   * Exports the value.   */  @Override  public void varExport(StringBuilder sb)  {    sb.append("array(");    boolean isFirst = true;    for (Entry entry = getHead(); entry != null; entry = entry._next) {      entry.getKey().varExport(sb);      sb.append(" => ");      entry.getValue().varExport(sb);      sb.append(", ");    }    sb.append(")");  }  /**   * Resets all numerical keys with the first index as base   *   * @param base  the initial index   * @param strict  if true, string keys are also reset   */  public boolean keyReset(long base, boolean strict)  {    Entry []entries;    entries = new Entry[getSize()];    int i = 0;    for (Entry entry = getHead(); entry != null; entry = entry._next) {      entries[i++] = entry;    }    clear();    for (int j = 0; j < entries.length; j++) {      Value key = entries[j].getKey();      if (! (key instanceof StringValue) || strict)        put(LongValue.create(base++), entries[j].getValue());      else        put(entries[j].getKey(), entries[j].getValue());    }    return true;  }  /**   * Test for equality   *   * @param rValue rhs ArrayValue to compare to   *   * @return true if this is equal to rValue, false otherwise   */  @Override  public boolean eq(Value rValue)  {    if (rValue == null)      return false;    for (Map.Entry<Value, Value> entry: entrySet()) {      Value entryValue = entry.getValue();      Value entryKey = entry.getKey();      Value rEntryValue = rValue.get(entryKey);      if ((rEntryValue instanceof ArrayValue) &&          !entryValue.eq((ArrayValue) rEntryValue))        return false;      if (! entryValue.eq(rEntryValue))        return false;    }    return true;  }  /**   * Test for ===   *   * @param rValue rhs ArrayValue to compare to   *   * @return true if this is equal to rValue, false otherwise   */  @Override  public boolean eql(Value rValue)  {    if (rValue == null)      return false;    else if (getSize() != rValue.getSize())      return false;    rValue = rValue.toValue();    if (! (rValue instanceof ArrayValue))      return false;    ArrayValue rArray = (ArrayValue) rValue;    Iterator<Map.Entry<Value,Value>> iterA = entrySet().iterator();    Iterator<Map.Entry<Value,Value>> iterB = rArray.entrySet().iterator();    while (iterA.hasNext() && iterB.hasNext()) {      Map.Entry<Value,Value> entryA = iterA.next();      Map.Entry<Value,Value> entryB = iterB.next();      if (! entryA.getKey().eql(entryB.getKey()))        return false;      if (! entryA.getValue().eql(entryB.getValue()))        return false;    }    if (iterA.hasNext() || iterB.hasNext())      return false;    else      return true;  }  @Override  public void varDumpImpl(Env env,                          WriteStream out,                          int depth,                          IdentityHashMap<Value, String> valueSet)    throws IOException  {    out.println("array(" + getSize() + ") {");    for (Map.Entry<Value,Value> mapEntry : entrySet()) {      varDumpEntry(env, out, depth + 1, valueSet, mapEntry);      out.println();    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?