index
int64
repo_id
string
file_path
string
content
string
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractMapInlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.collections.HashTrieMap; public abstract class AbstractMapInlet<K, V, O> implements MapInlet<K, V, O> { protected MapOutlet<K, V, ? extends O> input; protected HashTrieMap<K, KeyEffect> effects; protected int version; public AbstractMapInlet() { this.input = null; this.effects = HashTrieMap.empty(); this.version = -1; } @Override public MapOutlet<K, V, ? extends O> input() { return this.input; } @SuppressWarnings("unchecked") @Override public void bindInput(Outlet<? extends O> input) { if (input instanceof MapOutlet<?, ?, ?>) { bindInput((MapOutlet<K, V, ? extends O>) input); } else { throw new IllegalArgumentException(input.toString()); } } public void bindInput(MapOutlet<K, V, ? extends O> input) { if (this.input != null) { this.input.unbindOutput(this); } this.input = input; if (this.input != null) { this.input.bindOutput(this); } } @Override public void unbindInput() { if (this.input != null) { this.input.unbindOutput(this); } this.input = null; } @Override public void disconnectInputs() { final MapOutlet<K, V, ? extends O> input = this.input; if (input != null) { input.unbindOutput(this); this.input = null; input.disconnectInputs(); } } @Override public void disconnectOutputs() { // nop } @Override public void invalidateOutputKey(K key, KeyEffect effect) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; if (oldEffects.get(key) != effect) { willInvalidateOutputKey(key, effect); this.effects = oldEffects.updated(key, effect); this.version = -1; onInvalidateOutputKey(key, effect); didInvalidateOutputKey(key, effect); } } @Override public void invalidateOutput() { if (this.version >= 0) { willInvalidateOutput(); this.version = -1; onInvalidateOutput(); didInvalidateOutput(); } } @Override public void reconcileOutputKey(K key, int version) { if (this.version < 0) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; final KeyEffect effect = oldEffects.get(key); if (effect != null) { willReconcileOutputKey(key, effect, version); this.effects = oldEffects.removed(key); if (this.input != null) { this.input.reconcileInputKey(key, version); } onReconcileOutputKey(key, effect, version); didReconcileOutputKey(key, effect, version); } } } @Override public void reconcileOutput(int version) { if (this.version < 0) { willReconcileOutput(version); final Iterator<K> keys = this.effects.keyIterator(); while (keys.hasNext()) { reconcileOutputKey(keys.next(), version); } this.version = version; onReconcileOutput(version); didReconcileOutput(version); } } protected void willInvalidateOutputKey(K key, KeyEffect effect) { // stub } protected void onInvalidateOutputKey(K key, KeyEffect effect) { // stub } protected void didInvalidateOutputKey(K key, KeyEffect effect) { // stub } protected void willInvalidateOutput() { // stub } protected void onInvalidateOutput() { // stub } protected void didInvalidateOutput() { // stub } protected void willReconcileOutputKey(K key, KeyEffect effect, int version) { // stub } protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { // stub } protected void didReconcileOutputKey(K key, KeyEffect effect, int version) { // stub } protected void willReconcileOutput(int version) { // stub } protected void onReconcileOutput(int version) { // stub } protected void didReconcileOutput(int version) { // stub } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractMapInletMapOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.collections.HashTrieMap; import swim.util.Cursor; public abstract class AbstractMapInletMapOutlet<KI, KO, VI, VO, I, O> implements MapInletMapOutlet<KI, KO, VI, VO, I, O> { protected MapOutlet<KI, VI, ? extends I> input; protected HashTrieMap<KI, KeyEffect> outputEffects; protected HashTrieMap<KO, KeyEffect> inputEffects; protected HashTrieMap<KO, KeyOutlet<KO, VO>> outlets; protected Inlet<? super O>[] outputs; protected int version; public AbstractMapInletMapOutlet() { this.input = null; this.outputEffects = HashTrieMap.empty(); this.inputEffects = HashTrieMap.empty(); this.outlets = HashTrieMap.empty(); this.outputs = null; this.version = -1; } @Override public abstract boolean containsKey(KO key); @Override public abstract VO get(KO key); @Override public abstract O get(); @Override public abstract Iterator<KO> keyIterator(); @Override public MapOutlet<KI, VI, ? extends I> input() { return this.input; } @SuppressWarnings("unchecked") @Override public void bindInput(Outlet<? extends I> input) { if (input instanceof MapOutlet<?, ?, ?>) { bindInput((MapOutlet<KI, VI, ? extends I>) input); } else { throw new IllegalArgumentException(input.toString()); } } public void bindInput(MapOutlet<KI, VI, ? extends I> input) { if (this.input != null) { this.input.unbindOutput(this); } this.input = input; if (this.input != null) { this.input.bindOutput(this); } } @Override public void unbindInput() { if (this.input != null) { this.input.unbindOutput(this); } this.input = null; } @Override public void disconnectInputs() { if (this.outputs == null && this.outlets.isEmpty()) { final MapOutlet<KI, VI, ? extends I> input = this.input; if (input != null) { input.unbindOutput(this); this.input = null; input.disconnectInputs(); } } } @Override public Outlet<VO> outlet(KO key) { KeyOutlet<KO, VO> outlet = this.outlets.get(key); if (outlet == null) { outlet = new KeyOutlet<KO, VO>(this, key); this.outlets = this.outlets.updated(key, outlet); } return outlet; } @Override public Iterator<Inlet<? super O>> outputIterator() { return this.outputs != null ? Cursor.array(this.outputs) : Cursor.empty(); } @SuppressWarnings("unchecked") @Override public void bindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n + 1]; if (n > 0) { System.arraycopy(oldOutputs, 0, newOutputs, 0, n); } newOutputs[n] = output; this.outputs = newOutputs; } @SuppressWarnings("unchecked") @Override public void unbindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; for (int i = 0; i < n; i += 1) { if (oldOutputs[i] == output) { if (n > 1) { final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n - 1]; System.arraycopy(oldOutputs, 0, newOutputs, 0, i); System.arraycopy(oldOutputs, i + 1, newOutputs, i, (n - 1) - i); this.outputs = newOutputs; } else { this.outputs = null; } break; } } } @Override public void unbindOutputs() { final HashTrieMap<KO, KeyOutlet<KO, VO>> outlets = this.outlets; if (!outlets.isEmpty()) { this.outlets = HashTrieMap.empty(); final Iterator<KeyOutlet<KO, VO>> keyOutlets = outlets.valueIterator(); while (keyOutlets.hasNext()) { final KeyOutlet<KO, VO> keyOutlet = keyOutlets.next(); keyOutlet.unbindOutputs(); } } final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); } } } @Override public void disconnectOutputs() { if (this.input == null) { final HashTrieMap<KO, KeyOutlet<KO, VO>> outlets = this.outlets; if (!outlets.isEmpty()) { this.outlets = HashTrieMap.empty(); final Iterator<KeyOutlet<KO, VO>> keyOutlets = outlets.valueIterator(); while (keyOutlets.hasNext()) { final KeyOutlet<KO, VO> keyOutlet = keyOutlets.next(); keyOutlet.disconnectOutputs(); } } final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); output.disconnectOutputs(); } } } } @Override public void invalidateOutputKey(KI key, KeyEffect effect) { final HashTrieMap<KI, KeyEffect> oldOutputEffects = this.outputEffects; if (oldOutputEffects.get(key) != effect) { willInvalidateOutputKey(key, effect); this.outputEffects = oldOutputEffects.updated(key, effect); this.version = -1; onInvalidateOutputKey(key, effect); didInvalidateOutputKey(key, effect); } } @SuppressWarnings("unchecked") @Override public void invalidateInputKey(KO key, KeyEffect effect) { final HashTrieMap<KO, KeyEffect> oldInputEffects = this.inputEffects; if (oldInputEffects.get(key) != effect) { willInvalidateInputKey(key, effect); this.inputEffects = oldInputEffects.updated(key, effect); this.version = -1; onInvalidateInputKey(key, effect); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; if (output instanceof MapInlet<?, ?, ?>) { ((MapInlet<KO, VO, ? super O>) output).invalidateOutputKey(key, effect); } else { output.invalidateOutput(); } } final KeyOutlet<KO, VO> outlet = this.outlets.get(key); if (outlet != null) { outlet.invalidateInput(); } didInvalidateInputKey(key, effect); } } @Override public void invalidateOutput() { invalidate(); } @Override public void invalidateInput() { invalidate(); } public void invalidate() { if (this.version >= 0) { willInvalidate(); this.version = -1; onInvalidate(); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { this.outputs[i].invalidateOutput(); } final Iterator<KeyOutlet<KO, VO>> outlets = this.outlets.valueIterator(); while (outlets.hasNext()) { outlets.next().invalidateInput(); } didInvalidate(); } } @Override public void reconcileOutputKey(KI key, int version) { if (this.version < 0) { final HashTrieMap<KI, KeyEffect> oldOutputEffects = this.outputEffects; final KeyEffect effect = oldOutputEffects.get(key); if (effect != null) { willReconcileOutputKey(key, effect, version); this.outputEffects = oldOutputEffects.removed(key); if (this.input != null) { this.input.reconcileInputKey(key, version); } onReconcileOutputKey(key, effect, version); didReconcileOutputKey(key, effect, version); } } } @SuppressWarnings("unchecked") @Override public void reconcileInputKey(KO key, int version) { if (this.version < 0) { final HashTrieMap<KO, KeyEffect> oldInputEffects = this.inputEffects; final KeyEffect oldEffect = oldInputEffects.get(key); if (oldEffect != null) { final KeyEffect newEffect = willReconcileInputKey(key, oldEffect, version); if (oldEffect != newEffect) { invalidateInputKey(key, newEffect); } this.inputEffects = oldInputEffects.removed(key); onReconcileInputKey(key, newEffect, version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; if (output instanceof MapInlet<?, ?, ?>) { ((MapInlet<KO, VO, ? super O>) output).reconcileOutputKey(key, version); } } final KeyOutlet<KO, VO> outlet = this.outlets.get(key); if (outlet != null) { outlet.reconcileInput(version); } didReconcileInputKey(key, newEffect, version); } } } @Override public void reconcileOutput(int version) { reconcile(version); } @Override public void reconcileInput(int version) { reconcile(version); } public void reconcile(int version) { if (this.version < 0) { willReconcile(version); final Iterator<KI> outputKeys = this.outputEffects.keyIterator(); while (outputKeys.hasNext()) { reconcileOutputKey(outputKeys.next(), version); } final Iterator<KO> inputKeys = this.inputEffects.keyIterator(); while (inputKeys.hasNext()) { reconcileInputKey(inputKeys.next(), version); } this.version = version; onReconcile(version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { this.outputs[i].reconcileOutput(version); } didReconcile(version); } } protected void willInvalidateOutputKey(KI key, KeyEffect effect) { // stub } protected void onInvalidateOutputKey(KI key, KeyEffect effect) { // stub } protected void didInvalidateOutputKey(KI key, KeyEffect effect) { // stub } protected void willInvalidateInputKey(KO key, KeyEffect effect) { // stub } protected void onInvalidateInputKey(KO key, KeyEffect effect) { // stub } protected void didInvalidateInputKey(KO key, KeyEffect effect) { // stub } protected void willInvalidate() { // stub } protected void onInvalidate() { // stub } protected void didInvalidate() { // stub } protected void willReconcileOutputKey(KI key, KeyEffect effect, int version) { // stub } protected void onReconcileOutputKey(KI key, KeyEffect effect, int version) { // stub } protected void didReconcileOutputKey(KI key, KeyEffect effect, int version) { // stub } protected KeyEffect willReconcileInputKey(KO key, KeyEffect effect, int version) { return effect; } protected void onReconcileInputKey(KO key, KeyEffect effect, int version) { // stub } protected void didReconcileInputKey(KO key, KeyEffect effect, int version) { // stub } protected void willReconcile(int version) { // stub } protected void onReconcile(int version) { // stub } protected void didReconcile(int version) { // stub } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractMapInletOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.collections.HashTrieMap; import swim.util.Cursor; public abstract class AbstractMapInletOutlet<K, V, I, O> implements MapInletOutlet<K, V, I, O> { protected MapOutlet<K, V, ? extends I> input; protected HashTrieMap<K, KeyEffect> effects; protected Inlet<? super O>[] outputs; protected int version; public AbstractMapInletOutlet() { this.input = null; this.effects = HashTrieMap.empty(); this.outputs = null; this.version = -1; } @Override public abstract O get(); @Override public MapOutlet<K, V, ? extends I> input() { return this.input; } @SuppressWarnings("unchecked") @Override public void bindInput(Outlet<? extends I> input) { if (input instanceof MapOutlet<?, ?, ?>) { bindInput((MapOutlet<K, V, ? extends I>) input); } else { throw new IllegalArgumentException(input.toString()); } } public void bindInput(MapOutlet<K, V, ? extends I> input) { if (this.input != null) { this.input.unbindOutput(this); } this.input = input; if (this.input != null) { this.input.bindOutput(this); } } @Override public void unbindInput() { if (this.input != null) { this.input.unbindOutput(this); } this.input = null; } @Override public void disconnectInputs() { if (this.outputs == null) { final MapOutlet<K, V, ? extends I> input = this.input; if (input != null) { input.unbindOutput(this); this.input = null; input.disconnectInputs(); } } } @Override public Iterator<Inlet<? super O>> outputIterator() { return this.outputs != null ? Cursor.array(this.outputs) : Cursor.empty(); } @SuppressWarnings("unchecked") @Override public void bindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n + 1]; if (n > 0) { System.arraycopy(oldOutputs, 0, newOutputs, 0, n); } newOutputs[n] = output; this.outputs = newOutputs; } @SuppressWarnings("unchecked") @Override public void unbindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; for (int i = 0; i < n; i += 1) { if (oldOutputs[i] == output) { if (n > 1) { final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n - 1]; System.arraycopy(oldOutputs, 0, newOutputs, 0, i); System.arraycopy(oldOutputs, i + 1, newOutputs, i, (n - 1) - i); this.outputs = newOutputs; } else { this.outputs = null; } break; } } } @Override public void unbindOutputs() { final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); } } } @Override public void disconnectOutputs() { if (this.input == null) { final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); output.disconnectOutputs(); } } } } @SuppressWarnings("unchecked") @Override public void invalidateOutputKey(K key, KeyEffect effect) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; if (oldEffects.get(key) != effect) { willInvalidateOutputKey(key, effect); this.effects = oldEffects.updated(key, effect); this.version = -1; onInvalidateOutputKey(key, effect); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; output.invalidateOutput(); } didInvalidateOutputKey(key, effect); } } @Override public void invalidateOutput() { invalidate(); } @Override public void invalidateInput() { invalidate(); } public void invalidate() { if (this.version >= 0) { willInvalidate(); this.version = -1; onInvalidate(); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { this.outputs[i].invalidateOutput(); } didInvalidate(); } } @SuppressWarnings("unchecked") @Override public void reconcileOutputKey(K key, int version) { if (this.version < 0) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; final KeyEffect effect = oldEffects.get(key); if (effect != null) { willReconcileOutputKey(key, effect, version); this.effects = oldEffects.removed(key); if (this.input != null) { this.input.reconcileInputKey(key, version); } onReconcileOutputKey(key, effect, version); didReconcileOutputKey(key, effect, version); } } } @Override public void reconcileOutput(int version) { reconcile(version); } @Override public void reconcileInput(int version) { reconcile(version); } public void reconcile(int version) { if (this.version < 0) { willReconcile(version); final Iterator<K> keys = this.effects.keyIterator(); while (keys.hasNext()) { reconcileOutputKey(keys.next(), version); } this.version = version; onReconcile(version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { this.outputs[i].reconcileOutput(version); } didReconcile(version); } } protected void willInvalidateOutputKey(K key, KeyEffect effect) { // stub } protected void onInvalidateOutputKey(K key, KeyEffect effect) { // stub } protected void didInvalidateOutputKey(K key, KeyEffect effect) { // stub } protected void willInvalidate() { // stub } protected void onInvalidate() { // stub } protected void didInvalidate() { // stub } protected void willReconcileOutputKey(K key, KeyEffect effect, int version) { // stub } protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { // stub } protected void didReconcileOutputKey(K key, KeyEffect effect, int version) { // stub } protected void willReconcile(int version) { // stub } protected void onReconcile(int version) { // stub } protected void didReconcile(int version) { // stub } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractMapInoutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.collections.HashTrieMap; import swim.util.Cursor; public abstract class AbstractMapInoutlet<K, VI, VO, I, O> implements MapInoutlet<K, VI, VO, I, O> { protected MapOutlet<K, VI, ? extends I> input; protected HashTrieMap<K, KeyEffect> effects; protected HashTrieMap<K, KeyOutlet<K, VO>> outlets; protected Inlet<? super O>[] outputs; protected int version; public AbstractMapInoutlet() { this.input = null; this.effects = HashTrieMap.empty(); this.outlets = HashTrieMap.empty(); this.outputs = null; this.version = -1; } @Override public abstract boolean containsKey(K key); @Override public abstract VO get(K key); @Override public abstract O get(); @Override public abstract Iterator<K> keyIterator(); @Override public MapOutlet<K, VI, ? extends I> input() { return this.input; } @SuppressWarnings("unchecked") @Override public void bindInput(Outlet<? extends I> input) { if (input instanceof MapOutlet<?, ?, ?>) { bindInput((MapOutlet<K, VI, ? extends I>) input); } else { throw new IllegalArgumentException(input.toString()); } } public void bindInput(MapOutlet<K, VI, ? extends I> input) { if (this.input != null) { this.input.unbindOutput(this); } this.input = input; if (this.input != null) { this.input.bindOutput(this); } } @Override public void unbindInput() { if (this.input != null) { this.input.unbindOutput(this); } this.input = null; } @Override public void disconnectInputs() { if (this.outputs == null && this.outlets.isEmpty()) { final MapOutlet<K, VI, ? extends I> input = this.input; if (input != null) { input.unbindOutput(this); this.input = null; input.disconnectInputs(); } } } @Override public Outlet<VO> outlet(K key) { KeyOutlet<K, VO> outlet = this.outlets.get(key); if (outlet == null) { outlet = new KeyOutlet<K, VO>(this, key); this.outlets = this.outlets.updated(key, outlet); } return outlet; } @Override public Iterator<Inlet<? super O>> outputIterator() { return this.outputs != null ? Cursor.array(this.outputs) : Cursor.empty(); } @SuppressWarnings("unchecked") @Override public void bindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n + 1]; if (n > 0) { System.arraycopy(oldOutputs, 0, newOutputs, 0, n); } newOutputs[n] = output; this.outputs = newOutputs; } @SuppressWarnings("unchecked") @Override public void unbindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; for (int i = 0; i < n; i += 1) { if (oldOutputs[i] == output) { if (n > 1) { final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n - 1]; System.arraycopy(oldOutputs, 0, newOutputs, 0, i); System.arraycopy(oldOutputs, i + 1, newOutputs, i, (n - 1) - i); this.outputs = newOutputs; } else { this.outputs = null; } break; } } } @Override public void unbindOutputs() { final HashTrieMap<K, KeyOutlet<K, VO>> outlets = this.outlets; if (!outlets.isEmpty()) { this.outlets = HashTrieMap.empty(); final Iterator<KeyOutlet<K, VO>> keyOutlets = outlets.valueIterator(); while (keyOutlets.hasNext()) { final KeyOutlet<K, VO> keyOutlet = keyOutlets.next(); keyOutlet.unbindOutputs(); } } final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); } } } @Override public void disconnectOutputs() { if (this.input == null) { final HashTrieMap<K, KeyOutlet<K, VO>> outlets = this.outlets; if (!outlets.isEmpty()) { this.outlets = HashTrieMap.empty(); final Iterator<KeyOutlet<K, VO>> keyOutlets = outlets.valueIterator(); while (keyOutlets.hasNext()) { final KeyOutlet<K, VO> keyOutlet = keyOutlets.next(); keyOutlet.disconnectOutputs(); } } final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); output.disconnectOutputs(); } } } } @Override public void invalidateOutputKey(K key, KeyEffect effect) { invalidateKey(key, effect); } @Override public void invalidateInputKey(K key, KeyEffect effect) { invalidateKey(key, effect); } @SuppressWarnings("unchecked") public void invalidateKey(K key, KeyEffect effect) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; if (oldEffects.get(key) != effect) { willInvalidateKey(key, effect); this.effects = oldEffects.updated(key, effect); this.version = -1; onInvalidateKey(key, effect); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; if (output instanceof MapInlet<?, ?, ?>) { ((MapInlet<K, VO, ? super O>) output).invalidateOutputKey(key, effect); } else { output.invalidateOutput(); } } final KeyOutlet<K, VO> outlet = this.outlets.get(key); if (outlet != null) { outlet.invalidateInput(); } didInvalidateKey(key, effect); } } @Override public void invalidateOutput() { invalidate(); } @Override public void invalidateInput() { invalidate(); } public void invalidate() { if (this.version >= 0) { willInvalidate(); this.version = -1; onInvalidate(); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { this.outputs[i].invalidateOutput(); } final Iterator<KeyOutlet<K, VO>> outlets = this.outlets.valueIterator(); while (outlets.hasNext()) { outlets.next().invalidateInput(); } didInvalidate(); } } @Override public void reconcileOutputKey(K key, int version) { reconcileKey(key, version); } @Override public void reconcileInputKey(K key, int version) { reconcileKey(key, version); } @SuppressWarnings("unchecked") public void reconcileKey(K key, int version) { if (this.version < 0) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; final KeyEffect effect = oldEffects.get(key); if (effect != null) { willReconcileKey(key, effect, version); this.effects = oldEffects.removed(key); if (this.input != null) { this.input.reconcileInputKey(key, version); } onReconcileKey(key, effect, version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; if (output instanceof MapInlet<?, ?, ?>) { ((MapInlet<K, VO, ? super O>) output).reconcileOutputKey(key, version); } } final KeyOutlet<K, VO> outlet = this.outlets.get(key); if (outlet != null) { outlet.reconcileInput(version); } didReconcileKey(key, effect, version); } } } @Override public void reconcileOutput(int version) { reconcile(version); } @Override public void reconcileInput(int version) { reconcile(version); } public void reconcile(int version) { if (this.version < 0) { willReconcile(version); final Iterator<K> keys = this.effects.keyIterator(); while (keys.hasNext()) { reconcileKey(keys.next(), version); } this.version = version; onReconcile(version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { this.outputs[i].reconcileOutput(version); } didReconcile(version); } } protected void willInvalidateKey(K key, KeyEffect effect) { // stub } protected void onInvalidateKey(K key, KeyEffect effect) { // stub } protected void didInvalidateKey(K key, KeyEffect effect) { // stub } protected void willInvalidate() { // stub } protected void onInvalidate() { // stub } protected void didInvalidate() { // stub } protected void willReconcileKey(K key, KeyEffect effect, int version) { // stub } protected void onReconcileKey(K key, KeyEffect effect, int version) { // stub } protected void didReconcileKey(K key, KeyEffect effect, int version) { // stub } protected void willReconcile(int version) { // stub } protected void onReconcile(int version) { // stub } protected void didReconcile(int version) { // stub } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractMapOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.collections.HashTrieMap; import swim.util.Cursor; public abstract class AbstractMapOutlet<K, V, O> implements MapOutlet<K, V, O> { protected HashTrieMap<K, KeyEffect> effects; protected HashTrieMap<K, KeyOutlet<K, V>> outlets; protected Inlet<? super O>[] outputs; protected int version; public AbstractMapOutlet() { this.effects = HashTrieMap.empty(); this.outlets = HashTrieMap.empty(); this.outputs = null; this.version = -1; } @Override public abstract boolean containsKey(K key); @Override public abstract V get(K key); @Override public abstract O get(); @Override public abstract Iterator<K> keyIterator(); @Override public Outlet<V> outlet(K key) { KeyOutlet<K, V> outlet = this.outlets.get(key); if (outlet == null) { outlet = new KeyOutlet<K, V>(this, key); this.outlets = this.outlets.updated(key, outlet); } return outlet; } @Override public Iterator<Inlet<? super O>> outputIterator() { return this.outputs != null ? Cursor.array(this.outputs) : Cursor.empty(); } @SuppressWarnings("unchecked") @Override public void bindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n + 1]; if (n > 0) { System.arraycopy(oldOutputs, 0, newOutputs, 0, n); } newOutputs[n] = output; this.outputs = newOutputs; } @SuppressWarnings("unchecked") @Override public void unbindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; for (int i = 0; i < n; i += 1) { if (oldOutputs[i] == output) { if (n > 1) { final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n - 1]; System.arraycopy(oldOutputs, 0, newOutputs, 0, i); System.arraycopy(oldOutputs, i + 1, newOutputs, i, (n - 1) - i); this.outputs = newOutputs; } else { this.outputs = null; } break; } } } @Override public void unbindOutputs() { final HashTrieMap<K, KeyOutlet<K, V>> outlets = this.outlets; if (!outlets.isEmpty()) { this.outlets = HashTrieMap.empty(); final Iterator<KeyOutlet<K, V>> keyOutlets = outlets.valueIterator(); while (keyOutlets.hasNext()) { final KeyOutlet<K, V> keyOutlet = keyOutlets.next(); keyOutlet.unbindOutputs(); } } final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); } } } @Override public void disconnectOutputs() { final HashTrieMap<K, KeyOutlet<K, V>> outlets = this.outlets; if (!outlets.isEmpty()) { this.outlets = HashTrieMap.empty(); final Iterator<KeyOutlet<K, V>> keyOutlets = outlets.valueIterator(); while (keyOutlets.hasNext()) { final KeyOutlet<K, V> keyOutlet = keyOutlets.next(); keyOutlet.disconnectOutputs(); } } final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); output.disconnectOutputs(); } } } @Override public void disconnectInputs() { // nop } @SuppressWarnings("unchecked") @Override public void invalidateInputKey(K key, KeyEffect effect) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; if (oldEffects.get(key) != effect) { willInvalidateInputKey(key, effect); this.effects = oldEffects.updated(key, effect); this.version = -1; onInvalidateInputKey(key, effect); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; if (output instanceof MapInlet<?, ?, ?>) { ((MapInlet<K, V, ? super O>) output).invalidateOutputKey(key, effect); } else { output.invalidateOutput(); } } final KeyOutlet<K, V> outlet = this.outlets.get(key); if (outlet != null) { outlet.invalidateInput(); } didInvalidateInputKey(key, effect); } } @Override public void invalidateInput() { if (this.version >= 0) { willInvalidateInput(); this.version = -1; onInvalidateInput(); final int n = this.outputs != null ? this.outputs.length : 0; for (int i = 0; i < n; i += 1) { this.outputs[i].invalidateOutput(); } final Iterator<KeyOutlet<K, V>> outlets = this.outlets.valueIterator(); while (outlets.hasNext()) { outlets.next().invalidateInput(); } didInvalidateInput(); } } @SuppressWarnings("unchecked") @Override public void reconcileInputKey(K key, int version) { if (this.version < 0) { final HashTrieMap<K, KeyEffect> oldEffects = this.effects; final KeyEffect effect = oldEffects.get(key); if (effect != null) { willReconcileInputKey(key, effect, version); this.effects = oldEffects.removed(key); onReconcileInputKey(key, effect, version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { final Inlet<?> output = this.outputs[i]; if (output instanceof MapInlet<?, ?, ?>) { ((MapInlet<K, V, ? super O>) output).reconcileOutputKey(key, version); } } final KeyOutlet<K, V> outlet = this.outlets.get(key); if (outlet != null) { outlet.reconcileInput(version); } didReconcileInputKey(key, effect, version); } } } @Override public void reconcileInput(int version) { if (this.version < 0) { willReconcileInput(version); final Iterator<K> keys = this.effects.keyIterator(); while (keys.hasNext()) { reconcileInputKey(keys.next(), version); } this.version = version; onReconcileInput(version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { this.outputs[i].reconcileOutput(version); } didReconcileInput(version); } } protected void willInvalidateInputKey(K key, KeyEffect effect) { // stub } protected void onInvalidateInputKey(K key, KeyEffect effect) { // stub } protected void didInvalidateInputKey(K key, KeyEffect effect) { // stub } protected void willInvalidateInput() { // stub } protected void onInvalidateInput() { // stub } protected void didInvalidateInput() { // stub } protected void willReconcileInputKey(K key, KeyEffect effect, int version) { // stub } protected void onReconcileInputKey(K key, KeyEffect effect, int version) { // stub } protected void didReconcileInputKey(K key, KeyEffect effect, int version) { // stub } protected void willReconcileInput(int version) { // stub } protected void onReconcileInput(int version) { // stub } protected void didReconcileInput(int version) { // stub } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.util.Cursor; public abstract class AbstractOutlet<O> implements Outlet<O> { protected Inlet<? super O>[] outputs; protected int version; public AbstractOutlet() { this.outputs = null; this.version = -1; } @Override public abstract O get(); @Override public Iterator<Inlet<? super O>> outputIterator() { return this.outputs != null ? Cursor.array(this.outputs) : Cursor.empty(); } @SuppressWarnings("unchecked") @Override public void bindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; final int n = oldOutputs != null ? oldOutputs.length : 0; final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n + 1]; if (n > 0) { System.arraycopy(oldOutputs, 0, newOutputs, 0, n); } newOutputs[n] = output; this.outputs = newOutputs; } @SuppressWarnings("unchecked") @Override public void unbindOutput(Inlet<? super O> output) { final Inlet<? super O>[] oldOutputs = this.outputs; for (int i = 0, n = oldOutputs != null ? oldOutputs.length : 0; i < n; i += 1) { if (oldOutputs[i] == output) { if (n > 1) { final Inlet<? super O>[] newOutputs = (Inlet<? super O>[]) new Inlet<?>[n - 1]; System.arraycopy(oldOutputs, 0, newOutputs, 0, i); System.arraycopy(oldOutputs, i + 1, newOutputs, i, (n - 1) - i); this.outputs = newOutputs; } else { this.outputs = null; } break; } } } @Override public void unbindOutputs() { final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); } } } @Override public void disconnectOutputs() { final Inlet<? super O>[] outputs = this.outputs; if (outputs != null) { this.outputs = null; for (int i = 0, n = outputs.length; i < n; i += 1) { final Inlet<? super O> output = outputs[i]; output.unbindInput(); output.disconnectOutputs(); } } } @Override public void disconnectInputs() { // nop } @Override public void invalidateInput() { if (this.version >= 0) { willInvalidateInput(); this.version = -1; onInvalidateInput(); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { this.outputs[i].invalidateOutput(); } didInvalidateInput(); } } @Override public void reconcileInput(int version) { if (this.version < 0) { willReconcileInput(version); this.version = version; onReconcileInput(version); for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) { this.outputs[i].reconcileOutput(version); } didReconcileInput(version); } } protected void willInvalidateInput() { // stub } protected void onInvalidateInput() { // stub } protected void didInvalidateInput() { // stub } protected void willReconcileInput(int version) { // stub } protected void onReconcileInput(int version) { // stub } protected void didReconcileInput(int version) { // stub } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/AbstractStreamlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.lang.reflect.Field; import java.util.AbstractMap; import java.util.Map; public abstract class AbstractStreamlet<I, O> implements GenericStreamlet<I, O> { protected StreamletScope<? extends O> scope; protected StreamletContext context; protected int version; public AbstractStreamlet(StreamletScope<? extends O> scope) { this.scope = scope; this.version = -1; } public AbstractStreamlet() { this(null); } @Override public StreamletScope<? extends O> streamletScope() { return this.scope; } @Override public void setStreamletScope(StreamletScope<? extends O> scope) { this.scope = scope; } @Override public StreamletContext streamletContext() { if (this.context != null) { return this.context; } final StreamletScope<? extends O> scope = streamletScope(); if (scope != null) { return scope.streamletContext(); } return null; } @Override public void setStreamletContext(StreamletContext context) { this.context = context; } @Override public Inlet<I> inlet(String key) { return reflectInletKey(key, this, getClass()); } protected <I2 extends I> Inlet<I2> inlet() { return new StreamletInlet<I2>(this); } @Override public void bindInput(String key, Outlet<? extends I> input) { final Inlet<I> inlet = inlet(key); if (inlet == null) { throw new IllegalArgumentException(key.toString()); } inlet.bindInput(input); } @Override public void unbindInput(String key) { final Inlet<I> inlet = inlet(key); if (inlet == null) { throw new IllegalArgumentException(key.toString()); } inlet.unbindInput(); } @Override public Outlet<O> outlet(String key) { return reflectOutletKey(key, this, getClass()); } @SuppressWarnings("unchecked") protected <O2> Outlet<O2> outlet() { return new StreamletOutlet<O2>((Streamlet<I, ? extends O2>) this); } @SuppressWarnings("unchecked") protected <I2 extends I, O2> Inoutlet<I2, O2> inoutlet() { return new StreamletInoutlet<I2, O2>((Streamlet<? super I2, ? extends O2>) this); } @Override public void invalidate() { if (this.version >= 0) { willInvalidate(); this.version = -1; onInvalidate(); onInvalidateOutlets(); didInvalidate(); } } @Override public void reconcile(int version) { if (this.version < 0) { willReconcile(version); this.version = version; onReconcileInlets(version); onReconcile(version); onReconcileOutlets(version); didReconcile(version); } } public <I2 extends I> I2 getInput(Inlet<I2> inlet) { final Outlet<? extends I2> input = inlet.input(); if (input != null) { return input.get(); } return null; } @SuppressWarnings("unchecked") public <I2 extends I> I2 getInput(String key) { final Inlet<I2> inlet = (Inlet<I2>) inlet(key); if (inlet != null) { return getInput(inlet); } return null; } public <I2 extends I> I2 getInput(Inlet<I2> inlet, I2 orElse) { I2 input = getInput(inlet); if (input == null) { input = orElse; } return input; } public <I2 extends I> I2 getInput(String key, I2 orElse) { I2 input = getInput(key); if (input == null) { input = orElse; } return input; } @Override public O getOutput(Outlet<? super O> outlet) { return null; } public O getOutput(String key) { final Outlet<O> outlet = outlet(key); if (outlet != null) { return getOutput(outlet); } return null; } @Override public void disconnectInputs() { disconnectInputs(this, getClass()); } public static <I, O> void disconnectInputs(Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Inlet.class.isAssignableFrom(field.getType())) { disconnectInputField(streamlet, field); } } streamletClass = streamletClass.getSuperclass(); } } private static <I, O> void disconnectInputField(Streamlet<I, O> streamlet, Field field) { if (field.getAnnotation(Out.class) != null) { final Inlet<I> inlet = reflectInletField(streamlet, field); inlet.disconnectInputs(); } else if (field.getAnnotation(Inout.class) != null) { final Inoutlet<I, O> inoutlet = reflectInoutletField(streamlet, field); inoutlet.disconnectInputs(); } } @Override public void disconnectOutputs() { disconnectOutputs(this, getClass()); } public static <I, O> void disconnectOutputs(Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Outlet.class.isAssignableFrom(field.getType())) { disconnectOutputField(streamlet, field); } } streamletClass = streamletClass.getSuperclass(); } } private static <I, O> void disconnectOutputField(Streamlet<I, O> streamlet, Field field) { if (field.getAnnotation(Out.class) != null) { final Outlet<O> outlet = reflectOutletField(streamlet, field); outlet.disconnectOutputs(); } else if (field.getAnnotation(Inout.class) != null) { final Inoutlet<I, O> inoutlet = reflectInoutletField(streamlet, field); inoutlet.disconnectOutputs(); } } @Override public void willInvalidateInlet(Inlet<? extends I> inlet) { // stub } @Override public void didInvalidateInlet(Inlet<? extends I> inlet) { invalidate(); } @Override public void willReconcileInlet(Inlet<? extends I> inlet, int version) { // stub } @Override public void didReconcileInlet(Inlet<? extends I> inlet, int version) { reconcile(version); } @Override public void willInvalidateOutlet(Outlet<? super O> outlet) { // stub } @Override public void didInvalidateOutlet(Outlet<? super O> outlet) { // stub } @Override public void willReconcileOutlet(Outlet<? super O> outlet, int version) { // stub } @Override public void didReconcileOutlet(Outlet<? super O> outlet, int version) { // stub } protected void willInvalidate() { // stub } protected void onInvalidate() { // stub } protected void onInvalidateOutlets() { invalidateOutlets(this, getClass()); } public static <I, O> void invalidateOutlets(Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Outlet.class.isAssignableFrom(field.getType())) { invalidateOutletField(streamlet, field); } } streamletClass = streamletClass.getSuperclass(); } } private static <I, O> void invalidateOutletField(Streamlet<I, O> streamlet, Field field) { if (field.getAnnotation(Out.class) != null) { final Outlet<O> outlet = reflectOutletField(streamlet, field); outlet.invalidateInput(); } else if (field.getAnnotation(Inout.class) != null) { final Inoutlet<I, O> inoutlet = reflectInoutletField(streamlet, field); inoutlet.invalidateInput(); } } protected void didInvalidate() { // stub } protected void willReconcile(int version) { // stub } protected void onReconcileInlets(int version) { reconcileInlets(version, this, getClass()); } public static <I, O> void reconcileInlets(int version, Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Inlet.class.isAssignableFrom(field.getType())) { reconcileInletField(version, streamlet, field); } } streamletClass = streamletClass.getSuperclass(); } } private static <I, O> void reconcileInletField(int version, Streamlet<I, O> streamlet, Field field) { if (field.getAnnotation(In.class) != null) { final Inlet<I> inlet = reflectInletField(streamlet, field); inlet.reconcileOutput(version); } else if (field.getAnnotation(Inout.class) != null) { final Inoutlet<I, O> inoutlet = reflectInoutletField(streamlet, field); inoutlet.reconcileOutput(version); } } protected void onReconcile(int version) { // stub } protected void onReconcileOutlets(int version) { reconcileOutlets(version, this, getClass()); } public static <I, O> void reconcileOutlets(int version, Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Outlet.class.isAssignableFrom(field.getType())) { reconcileOutletField(version, streamlet, field); } } streamletClass = streamletClass.getSuperclass(); } } private static <I, O> void reconcileOutletField(int version, Streamlet<I, O> streamlet, Field field) { if (field.getAnnotation(Out.class) != null) { final Outlet<O> outlet = reflectOutletField(streamlet, field); outlet.reconcileInput(version); } else if (field.getAnnotation(Inout.class) != null) { final Inoutlet<I, O> inoutlet = reflectInoutletField(streamlet, field); inoutlet.reconcileInput(version); } } protected void didReconcile(int version) { // stub } public static <I, O> int reflectInletCount(Class<?> streamletClass) { int count = 0; while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Inlet.class.isAssignableFrom(field.getType())) { final In in = field.getAnnotation(In.class); if (in != null) { count += 1; continue; } final Inout inout = field.getAnnotation(Inout.class); if (inout != null) { count += 1; continue; } } } streamletClass = streamletClass.getSuperclass(); } return count; } public static <I, O> int reflectOutletCount(Class<?> streamletClass) { int count = 0; while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Outlet.class.isAssignableFrom(field.getType())) { final Out out = field.getAnnotation(Out.class); if (out != null) { count += 1; continue; } final Inout inout = field.getAnnotation(Inout.class); if (inout != null) { count += 1; continue; } } } streamletClass = streamletClass.getSuperclass(); } return count; } public static <I, O> Map.Entry<String, Inlet<I>> reflectInletIndex(int index, Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Inlet.class.isAssignableFrom(field.getType())) { final In in = field.getAnnotation(In.class); if (in != null) { if (index == 0) { return new AbstractMap.SimpleImmutableEntry<String, Inlet<I>>(field.getName(), reflectInletField(streamlet, field)); } index -= 1; continue; } final Inout inout = field.getAnnotation(Inout.class); if (inout != null) { if (index == 0) { return new AbstractMap.SimpleImmutableEntry<String, Inlet<I>>(field.getName(), reflectInoutletField(streamlet, field)); } index -= 1; continue; } } } streamletClass = streamletClass.getSuperclass(); } return null; } public static <I, O> Map.Entry<String, Outlet<O>> reflectOutletIndex(int index, Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Outlet.class.isAssignableFrom(field.getType())) { final Out out = field.getAnnotation(Out.class); if (out != null) { if (index == 0) { return new AbstractMap.SimpleImmutableEntry<String, Outlet<O>>(field.getName(), reflectOutletField(streamlet, field)); } index -= 1; continue; } final Inout inout = field.getAnnotation(Inout.class); if (inout != null) { if (index == 0) { return new AbstractMap.SimpleImmutableEntry<String, Outlet<O>>(field.getName(), reflectInoutletField(streamlet, field)); } index -= 1; continue; } } } streamletClass = streamletClass.getSuperclass(); } return null; } public static <I, O> Inlet<I> reflectInletKey(String key, Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Inlet.class.isAssignableFrom(field.getType())) { final Inlet<I> inlet = reflectInletKeyField(key, streamlet, field); if (inlet != null) { return inlet; } } } streamletClass = streamletClass.getSuperclass(); } return null; } private static <I, O> Inlet<I> reflectInletKeyField(String key, Streamlet<I, O> streamlet, Field field) { final In in = field.getAnnotation(In.class); if (in != null) { final String name = in.value(); if (name.equals(key) || name.isEmpty() && field.getName().equals(key)) { return reflectInletField(streamlet, field); } } final Inout inout = field.getAnnotation(Inout.class); if (inout != null) { final String name = inout.value(); if (name.equals(key) || name.isEmpty() && field.getName().equals(key)) { return reflectInoutletField(streamlet, field); } } return null; } public static <I, O> Outlet<O> reflectOutletKey(String key, Streamlet<I, O> streamlet, Class<?> streamletClass) { while (streamletClass != null) { final Field[] fields = streamletClass.getDeclaredFields(); for (Field field : fields) { if (Outlet.class.isAssignableFrom(field.getType())) { final Outlet<O> outlet = reflectOutletKeyField(key, streamlet, field); if (outlet != null) { return outlet; } } } streamletClass = streamletClass.getSuperclass(); } return null; } @SuppressWarnings("unchecked") private static <I, O> Outlet<O> reflectOutletKeyField(String key, Streamlet<I, O> streamlet, Field field) { final Out out = field.getAnnotation(Out.class); if (out != null) { final String name = out.value(); if (name.equals(key) || name.isEmpty() && field.getName().equals(key)) { return reflectOutletField(streamlet, field); } } final Inout inout = field.getAnnotation(Inout.class); if (inout != null) { final String name = inout.value(); if (name.equals(key) || name.isEmpty() && field.getName().equals(key)) { return reflectInoutletField(streamlet, field); } } return null; } public static <I, O> Inlet<I> reflectInletField(Streamlet<I, O> streamlet, Field field) { field.setAccessible(true); if (MapInlet.class.isAssignableFrom(field.getType())) { return reflectMapInletField(streamlet, field); } else { return reflectValueInletField(streamlet, field); } } @SuppressWarnings("unchecked") private static <I, O> Inlet<I> reflectValueInletField(Streamlet<I, O> streamlet, Field field) { try { Inlet<I> inlet = (Inlet<I>) field.get(streamlet); if (inlet == null) { if (streamlet instanceof AbstractStreamlet<?, ?>) { inlet = ((AbstractStreamlet<I, O>) streamlet).inlet(); } else { inlet = new StreamletInlet<I>(streamlet); } field.set(streamlet, inlet); } return inlet; } catch (IllegalAccessException cause) { throw new StreamletException(cause); } } private static <I, O> Inlet<I> reflectMapInletField(Streamlet<I, O> streamlet, Field field) { return null; // TODO } public static <I, O> Outlet<O> reflectOutletField(Streamlet<I, O> streamlet, Field field) { field.setAccessible(true); if (MapOutlet.class.isAssignableFrom(field.getType())) { return reflectMapOutletField(streamlet, field); } else { return reflectValueOutletField(streamlet, field); } } @SuppressWarnings("unchecked") private static <I, O> Outlet<O> reflectValueOutletField(Streamlet<I, O> streamlet, Field field) { try { Outlet<O> outlet = (Outlet<O>) field.get(streamlet); if (outlet == null) { if (streamlet instanceof AbstractStreamlet<?, ?>) { outlet = ((AbstractStreamlet<I, O>) streamlet).outlet(); } else { outlet = new StreamletOutlet<O>(streamlet); } field.set(streamlet, outlet); } return outlet; } catch (IllegalAccessException cause) { throw new StreamletException(cause); } } private static <I, O> Outlet<O> reflectMapOutletField(Streamlet<I, O> streamlet, Field field) { return null; // TODO } public static <I, O> Inoutlet<I, O> reflectInoutletField(Streamlet<I, O> streamlet, Field field) { field.setAccessible(true); if (MapInoutlet.class.isAssignableFrom(field.getType())) { return reflectMapInoutletField(streamlet, field); } else { return reflectValueInoutletField(streamlet, field); } } @SuppressWarnings("unchecked") private static <I, O> Inoutlet<I, O> reflectValueInoutletField(Streamlet<I, O> streamlet, Field field) { try { Inoutlet<I, O> inoutlet = (Inoutlet<I, O>) field.get(streamlet); if (inoutlet == null) { if (streamlet instanceof AbstractStreamlet<?, ?>) { inoutlet = ((AbstractStreamlet<I, O>) streamlet).inoutlet(); } else { inoutlet = new StreamletInoutlet<I, O>(streamlet); } field.set(streamlet, inoutlet); } return inoutlet; } catch (IllegalAccessException cause) { throw new StreamletException(cause); } } private static <I, O> Inoutlet<I, O> reflectMapInoutletField(Streamlet<I, O> streamlet, Field field) { return null; // TODO } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/GenericStreamlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public interface GenericStreamlet<I, O> extends Streamlet<I, O> { O getOutput(Outlet<? super O> outlet); void willInvalidateInlet(Inlet<? extends I> inlet); void didInvalidateInlet(Inlet<? extends I> inlet); void willReconcileInlet(Inlet<? extends I> inlet, int version); void didReconcileInlet(Inlet<? extends I> inlet, int version); void willInvalidateOutlet(Outlet<? super O> outlet); void didInvalidateOutlet(Outlet<? super O> outlet); void willReconcileOutlet(Outlet<? super O> outlet, int version); void didReconcileOutlet(Outlet<? super O> outlet, int version); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/In.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface In { String value() default ""; }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/Inlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * Input connector into a {@link Streamlet}. An {@code Inlet} represents a * source from which a {@code Streamlet} acquires state. * <p> * In order for an {@code Inlet} to provide state to its {@code Streamlet}, * it must bind to an {@link #input() input} source. The input source of an * {@code Inlet} is an {@link Outlet} of some other {@code Streamlet}. The * {@link #bindInput(Outlet)} method &quot;plugs&quot; the {@code Inlet} into * an {@code Outlet}. The {@link #unbindInput()} method &quot;unplugs&quot; * the {@code Inlet} from its connected {@code Outlet}. * <p> * The state of an {@code Inlet} has an integral <em>version</em>. When its * version is negative, the state of the {@code Inlet} is considered * <em>invalid</em>. When any state on which an {@code Inlet} transitively * depends changes, the {@code Inlet} will be {@link #invalidateOutput() * invalidated}. Invalidation does not immediately cause an {@code Inlet} to * recompute its state. Instead, a separate {@link #reconcileOutput(int)} step * causes all of the invalid paths in the dataflow graph passing through the * {@code Inlet} to reconcile their state. */ public interface Inlet<I> { /** * Returns the {@code Outlet} from which this {@code Inlet} acquires its * state; returns {@code null} if this {@code Inlet} is disconnected. */ Outlet<? extends I> input(); /** * Connects this {@code Inlet} to an {@code Outlet} from which it will * acquire its state. If this {@code Inlet} is already connected, it will * first disconnect from its existing input. Then, after updating its {@link * #input() input} property, the {@code Inlet} will invoke {@link * Outlet#bindOutput(Inlet)} on its new {@code input}. */ void bindInput(Outlet<? extends I> input); /** * Disconnects this {@code Inlet} from its input {@code Outlet}, if * connected. After setting its {@link #input() input} property to {@code * null}, the {@code Inlet} will invoke {@link Outlet#unbindOutput(Inlet)} * on its old input, if defined. */ void unbindInput(); /** * Disconnects all {@code Inlet}s dominated by this {@code Inlet} in the * dataflow dependency graph. Used to recursively clean up chains of * combinators terminating at this {@code Inlet}. */ void disconnectInputs(); /** * Disconnects all {@code Outlet}s dominated by this {@code Inlet} in the * dataflow graph. Used to recursively clean up chains of combinators * passing through this {@code Inlet}. */ void disconnectOutputs(); /** * Marks this {@code Inlet}—and the {@code Streamlet} to which this {@code * Inlet} is attached—as having stale state. Invalidating an {@code Inlet} * will recursively invalidate all streamlets that transitively depend on the * state of this {@code Inlet}. Invalidating an {@code Inlet} does not cause * its state to be recomputed. A subsequent {@link #reconcileOutput(int)} * call will reconcile the state of the {@code Inlet}. */ void invalidateOutput(); /** * Reconciles the state of this {@code Inlet}, if the version of this {@code * Inlet}'s state differs from the target {@code version}. To reconcile its * state, the {@code Inlet} first invokes {@link Outlet#reconcileInput(int)} * on its {@link #input() input}, to ensure that its input is up-to-date. It * then invokes {@link Streamlet#reconcile(int)} on the {@code Streamlet} to * which it's attached, causing the {@code Streamlet} to reconcile its own * state. */ void reconcileOutput(int version); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/Inout.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Inout { String value() default ""; }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/Inoutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public interface Inoutlet<I, O> extends Inlet<I>, Outlet<O> { /** * Disconnects all {@code Inlet}s dominated by this {@code Inoutlet} in the * dataflow dependency graph. Used to recursively clean up chains of * combinators passing through this {@code Inoutlet}. */ @Override void disconnectInputs(); /** * Disconnects all {@code Inlets}s dominated by this {@code Inoutlet} in the * dataflow graph. Used to recursively clean up chains of combinators * passing through this {@code Inoutlet}. */ @Override void disconnectOutputs(); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/KeyEffect.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public enum KeyEffect { UPDATE, REMOVE; }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/KeyOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public class KeyOutlet<K, V> extends AbstractOutlet<V> { protected final MapOutlet<? super K, ? extends V, ?> input; protected final K key; public KeyOutlet(MapOutlet<? super K, ? extends V, ?> input, K key) { this.input = input; this.key = key; } public MapOutlet<? super K, ? extends V, ?> input() { return this.input; } public K key() { return this.key; } @Override public V get() { return this.input.get(this.key); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapInlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * Input connector into a {@link Streamlet} for a key-value map state. */ public interface MapInlet<K, V, I> extends Inlet<I> { /** * Marks this {@code MapInlet} as needing an {@code effect} applied to a * given {@code key}. Invalidating an individual key invalidates the entire * state of the {@code Inlet}. But only the invalidated keys need to be * updated in order to reconcile the overall state of the {@code Inlet}. */ void invalidateOutputKey(K key, KeyEffect effect); /** * Reconciles the state of an individual {@code key} in this {@code MapInlet}, * if the version of this {@code MapInlet}'s state differs from the target * {@code version}. To reconcile the state of a key, the {@code MapInlet} * first invokes {@link MapOutlet#reconcileInputKey(Object, int)} on its * {@link #input() input}, if its input is a {@link MapOutlet}, or it invokes * {@link Outlet#reconcileInput(int)}, if its input is not a {@code * MapOutlet}. Then, if all invalid keys have been reconciled, the {@code * MapInlet} invokes {@link Streamlet#reconcile(int)} on its attached * streamlet. */ void reconcileOutputKey(K key, int version); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapInletMapOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public interface MapInletMapOutlet<KI, KO, VI, VO, I, O> extends MapInlet<KI, VI, I>, MapOutlet<KO, VO, O> { }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapInletOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public interface MapInletOutlet<K, V, I, O> extends MapInlet<K, V, I>, Outlet<O> { }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapInoutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public interface MapInoutlet<K, VI, VO, I, O> extends Inoutlet<I, O>, MapInlet<K, VI, I>, MapOutlet<K, VO, O> { }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapInput.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import java.util.Map; import swim.collections.HashTrieMap; public class MapInput<K, V> extends AbstractMapOutlet<K, V, Map<K, V>> { protected HashTrieMap<K, V> state; public MapInput(HashTrieMap<K, V> state) { this.state = state; final Iterator<K> keys = state.keyIterator(); while (keys.hasNext()) { this.effects = this.effects.updated(keys.next(), KeyEffect.UPDATE); } } public MapInput() { this(HashTrieMap.empty()); } @Override public boolean containsKey(K key) { return this.state.containsKey(key); } @Override public V get(K key) { return this.state.get(key); } public V put(K key, V newValue) { final V oldValue = this.state.get(key); this.state = this.state.updated(key, newValue); invalidateInputKey(key, KeyEffect.UPDATE); return oldValue; } public boolean removeKey(K key) { final HashTrieMap<K, V> oldState = this.state; final HashTrieMap<K, V> newState = oldState.removed(key); if (oldState != newState) { this.state = newState; invalidateInputKey(key, KeyEffect.REMOVE); return true; } else { return false; } } @Override public Map<K, V> get() { return this.state; } @Override public Iterator<K> keyIterator() { return this.state.keyIterator(); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import java.util.Map; import swim.streamlet.combinator.FilterFieldsCombinator; import swim.streamlet.combinator.MapFieldValuesCombinator; import swim.streamlet.combinator.MemoizeMapCombinator; import swim.streamlet.combinator.ReduceFieldsCombinator; import swim.streamlet.combinator.WatchFieldsCombinator; import swim.streamlet.function.FilterFieldsFunction; import swim.streamlet.function.MapFieldValuesFunction; import swim.streamlet.function.WatchFieldsFunction; import swim.util.CombinerFunction; /** * Output connector from a {@link Streamlet} for a key-value map state. */ public interface MapOutlet<K, V, O> extends Outlet<O> { /** * Returns {@code true} if the current state of this {@code MapOutlet} * contains the given {@code key}; otherwise returns {@code false}. */ boolean containsKey(K key); /** * Returns the value assocaited with the given {@code key} in the current * state of this {@code MapOutlet}, if defined; otherwise returns {@code * null}. */ V get(K key); /** * Returns an {@code Iterator} over the keys in the current state of this * {@code MapOutlet}. */ Iterator<K> keyIterator(); /** * Returns an {@code Outlet} that updates when the specified {@code key} * updates. */ Outlet<V> outlet(K key); /** * Marks this {@code MapOutlet} as needing an {@code effect} applied to a * given {@code key}. Invalidating an individual key invalidates the entire * state of the {@code Outlet}. But only the invalidated keys need to be * updated in order to reconcile the overall state of the {@code Outlet}. */ void invalidateInputKey(K key, KeyEffect effect); /** * Reconciles the state of an individual {@code key} in this {@code * MapOutlet}, if the version of this {@code MapOutlet}'s state differs from * the target {@code version}. To reconcile the state of a key, the {@code * MapOutlet} first invokes {@link Streamlet#reconcile(int)} on its attached * streamlets. Then, for each dependent output, it invokes {@link * MapInlet#reconcileOutputKey(Object, int)}, if the dependent output is a * {@link MapInlet}, or it invokes {@link Inlet#reconcileOutput(int)}, if the * dependent output is not a {@code MapInlet}. */ void reconcileInputKey(K key, int version); @Override default MapOutlet<K, V, O> memoize() { final MemoizeMapCombinator<K, V, O> combinator = new MemoizeMapCombinator<K, V, O>(); combinator.bindInput(this); return combinator; } default MapOutlet<K, V, ? extends Map<K, V>> filter(FilterFieldsFunction<? super K, ? super V> func) { final FilterFieldsCombinator<K, V, O> combinator = new FilterFieldsCombinator<K, V, O>(func); combinator.bindInput(this); return combinator; } default <V2> MapOutlet<K, V2, ? extends Map<K, V2>> map(MapFieldValuesFunction<? super K, ? super V, V2> func) { final MapFieldValuesCombinator<K, V, V2, O> combinator = new MapFieldValuesCombinator<K, V, V2, O>(func); combinator.bindInput(this); return combinator; } default <U> Outlet<U> reduce(U identity, CombinerFunction<? super V, U> accumulator, CombinerFunction<U, U> combiner) { final ReduceFieldsCombinator<K, V, O, U> combinator = new ReduceFieldsCombinator<K, V, O, U>(identity, accumulator, combiner); combinator.bindInput(this); return combinator; } default MapOutlet<K, V, O> watch(WatchFieldsFunction<? super K, ? super V> func) { final WatchFieldsCombinator<K, V, O> combinator = new WatchFieldsCombinator<K, V, O>(func); combinator.bindInput(this); return this; } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/MapOutput.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Map; import swim.collections.HashTrieMap; public class MapOutput<K, V> extends AbstractMapInlet<K, V, Map<K, V>> { protected HashTrieMap<K, V> state; public MapOutput() { this.state = HashTrieMap.empty(); } public Map<K, V> get() { return this.state; } @Override protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { if (effect == KeyEffect.UPDATE) { if (this.input != null) { final V value = this.input.get(key); if (value != null) { this.state = this.state.updated(key, value); } else { this.state = this.state.removed(key); } } } else if (effect == KeyEffect.REMOVE) { this.state = this.state.removed(key); } } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/Out.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Out { String value() default ""; }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/Outlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import java.util.Iterator; import swim.streamlet.combinator.MapValueCombinator; import swim.streamlet.combinator.MemoizeValueCombinator; import swim.streamlet.combinator.WatchValueCombinator; import swim.streamlet.function.MapValueFunction; import swim.streamlet.function.WatchValueFunction; /** * Output connector from a {@link Streamlet}. An {@code Outlet} represents a * sink to which a {@code Streamlet} provides state. * <p> * An {@code Outlet} has a one-to-many relationship with a set of output sinks. * An output sink of an {@code Outlet} is an {@link Inlet} of some other {@code * Streamlet}. The {@link #bindOutput(Inlet)} method &quot;plugs&quot; an {@code * Inlet} into the {@code Outlet}. The {@link #unbindOutput(Inlet)} method * &quot;unplugs&quot; an {@code Inlet} from the {@code Outlet}. */ public interface Outlet<O> { /** * Returns the current state of this {@code Outlet}. */ O get(); /** * Returns an {@code Iterator} over the set of {@code Inlet}s that depend on * the state of this {@code Outlet}. */ Iterator<? extends Inlet<? super O>> outputIterator(); /** * Adds an {@code output} to the set of {@code Inlet}s that depend on the * state of this {@code Outlet}. The {@code output} will be invalidated when * the state of this {@code Outlet} is invalidated, and updated when this * {@code Outlet} is updated. */ void bindOutput(Inlet<? super O> output); /** * Removes an {@code output} from the set of {@code Inlet}s that depend on * the state of this {@code Outlet}. */ void unbindOutput(Inlet<? super O> output); /** * Disconnects all outputs from this {@code Outlet} by invoking {@link * Inlet#unbindInput()} on each {@code Inelt} that depends on the state of * this {@code Outlet}. */ void unbindOutputs(); /** * Disconnects all {@code Outlet}s dominated by this {@code Outlet} in the * dataflow graph. Used to recursively clean up chains of combinators * originating from this {@code Inlet}. */ void disconnectOutputs(); /** * Disconnects all {@code Inlet}s dominated by this {@code Outlet} in the * dataflow dependency graph. Used to recursively clean up chains of * combinators passing through this {@code Outlet}. */ void disconnectInputs(); /** * Marks this {@code Outlet}—and all {@link #outputIterator() outputs} that * depend on the state of this {@code Outlet}—as having stale state. */ void invalidateInput(); /** * Reconciles the state of this {@code Outlet}, if the version of this {@code * Outlet}'s state differs from the target {@code version}. To reconcile its * state, the {@code Outlet} first invokes {@link Streamlet#reconcile(int)} * on the {@code Streamlet} to which it's attached. It then invokes {@link * Inlet#reconcileOutput(int)} on each of its dependent {@link * #outputIterator() outputs}. */ void reconcileInput(int version); default Outlet<O> memoize() { final MemoizeValueCombinator<O> combinator = new MemoizeValueCombinator<O>(); combinator.bindInput(this); return combinator; } default <O2> Outlet<O2> map(MapValueFunction<? super O, O2> func) { final MapValueCombinator<O, O2> combinator = new MapValueCombinator<O, O2>(func); combinator.bindInput(this); return combinator; } default Outlet<O> watch(WatchValueFunction<? super O> func) { final WatchValueCombinator<O> combinator = new WatchValueCombinator<O>(func); combinator.bindInput(this); return this; } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/OutletInlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * An {@code Inlet} that invalidates a parameterized {@code Outlet} whenever * the {@code Inlet} is invalidated, and that updates the parameterized {@code * Outlet} whenever the {@code Inlet} updates. */ public class OutletInlet<I> extends AbstractInlet<I> { protected final Outlet<?> outlet; public OutletInlet(Outlet<?> outlet) { this.outlet = outlet; } public Outlet<?> outlet() { return this.outlet; } @Override protected void onInvalidateOutput() { this.outlet.invalidateInput(); } @Override protected void onReconcileOutput(int version) { this.outlet.reconcileInput(version); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/OutletMapInlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * A {@code MapInlet} that invalidates a parameterized {@code Outlet} whenever * the {@code MapInlet} is invalidated, and that updates the parameterized * {@code Outlet} whenever the {@code MapInlet} updates. */ public class OutletMapInlet<K, V, O> extends AbstractMapInlet<K, V, O> { protected final Outlet<?> outlet; public OutletMapInlet(Outlet<?> outlet) { this.outlet = outlet; } public Outlet<?> outlet() { return this.outlet; } @Override protected void onInvalidateOutputKey(K key, KeyEffect effect) { this.outlet.invalidateInput(); } @Override protected void onInvalidateOutput() { this.outlet.invalidateInput(); } @Override protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { this.outlet.reconcileInput(version); } @Override protected void onReconcileOutput(int version) { this.outlet.reconcileInput(version); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/Streamlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * Stateful node in a dataflow graph that uses the state of its {@link Inlet * Inlets} to compute the state of its {@link Outlet Outlets}. */ public interface Streamlet<I, O> extends StreamletScope<O> { /** * Returns the lexically scoped parent of this {@code Streamlet}. Returns * {@code null} if this {@code Streamlet} has no lexical parent. */ @Override StreamletScope<? extends O> streamletScope(); /** * Sets the lexically scoped parent of this {@code Streamlet}. */ void setStreamletScope(StreamletScope<? extends O> scope); /** * Returns the environment in which this {@code Streamlet} operates. */ @Override StreamletContext streamletContext(); /** * Sets the environment in which this {@code Streamlet} operates. */ void setStreamletContext(StreamletContext context); /** * Returns the {@code Inlet} to this {@code Streamlet} identified by the * given {@code key}; returns {@code null} if this {@code Streamlet} has no * such {@code Inlet}. */ Inlet<I> inlet(String key); /** * Connects the {@code Inlet} of this {@code Streamlet}, identified by the * given {@code key}, to the {@code input} from which the {@code Inlet} * should acquire its state. Delegates to {@link Inlet#bindInput(Outlet)} on * the identified {@code Inlet}. * * @throws IllegalArgumentException if this {@code Streamlet} has no {@code * Inlet} with the given {@code key}. */ void bindInput(String key, Outlet<? extends I> input); /** * Disconnects the {@code Inlet} of this {@code Streamlet}, identified by the * given {@code key}, from its {@link Inlet#input() input} {@code Outlet}, * if connected. Delegates to {@link Inlet#unbindInput()} on the * identified {@code Inlet}. * * @throws IllegalArgumentException if this {@code Streamlet} has no {@code * Inlet} with the given {@code key}. */ void unbindInput(String key); /** * Returns the {@code Outlet} of this {@code Streamlet} identified by the * given {@code key}; returns {@code null} if this {@code Streamlet} has no * such {@code Outlet}. */ @Override Outlet<O> outlet(String key); /** * Disconnects all {@code Inlet}s dominated by this {@code Streamlet} in the * dataflow dependency graph. Used to recursively clean up chains of * combinators terminating at this {@code Streamlet}. */ void disconnectInputs(); /** * Disconnects all {@code Inlets}s dominated by this {@code Streamlet} in the * dataflow graph. Used to recursively clean up chains of combinators * originating from this {@code Streamlet}. */ void disconnectOutputs(); /** * Marks this {@code Streamlet}—and all of its outlets—as having stale state. * Invalidating a {@code Streamlet} will recursively invalidate all * streamlets that transitively depend on the state of this {@code Streamlet}. * Invalidating a {@code Streamlet} does not cause its state to be * recomputed. A subsequent {@link #reconcile(int)} call will reconcile the * state of the {@code Streamlet}. */ void invalidate(); /** * Reconciles the state of this {@code Streamlet}, if the version of this * {@code Streamlet}'s state differs from the target {@code version}. * To reconcile its state, the {@code Streamlet} first invokes {@link * Inlet#reconcileOutput(int)} on each of its inlets, to ensure that its * input states are up-to-date. It then recomputes its own state, in an * implementation defined manner. Finally, it invokes {@link * Outlet#reconcileInput(int)} on its outlets, causing all transitively * dependent streamlets to reconcile their own state. */ void reconcile(int version); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/StreamletContext.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; import swim.concurrent.Schedule; import swim.concurrent.Stage; import swim.util.Log; /** * Environment in which a {@link Streamlet} executes. */ public interface StreamletContext extends Log { /** * Returns the {@code Schedule} with which the {@code Streamlet} can set timers. */ Schedule schedule(); /** * Returns the {@code Stage} on which the {@code Streamlet} can execute tasks. */ Stage stage(); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/StreamletException.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public class StreamletException extends RuntimeException { private static final long serialVersionUID = 1L; public StreamletException(String message, Throwable cause) { super(message, cause); } public StreamletException(String message) { super(message); } public StreamletException(Throwable cause) { super(cause); } public StreamletException() { super(); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/StreamletInlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * An {@code Inlet} that invalidates a parameterized {@code Streamlet} whenever * the {@code Inlet} is invalidated, and that updates the parameterized {@code * Streamlet} whenever the {@code Inlet} updates. */ public class StreamletInlet<I> extends AbstractInlet<I> { protected final Streamlet<? super I, ?> streamlet; public StreamletInlet(Streamlet<? super I, ?> streamlet) { this.streamlet = streamlet; } public Streamlet<? super I, ?> streamlet() { return this.streamlet; } @Override protected void willInvalidateOutput() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ?>) this.streamlet).willInvalidateInlet(this); } } @Override protected void didInvalidateOutput() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ?>) this.streamlet).didInvalidateInlet(this); } else { this.streamlet.invalidate(); } } @Override protected void willReconcileOutput(int version) { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ?>) this.streamlet).willReconcileInlet(this, version); } } @Override protected void didReconcileOutput(int version) { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ?>) this.streamlet).didReconcileInlet(this, version); } else { this.streamlet.reconcile(version); } } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/StreamletInoutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * An {@code Inoutlet} that invalidates a parameterized {@code Streamlet} * whenever the {@code Inoutlet} is invalidated, that updates the parameterized * {@code Streamlet} whenever the {@code Inoutlet} updates, and which gets its * state from the parameterized {@code Streamlet}. */ public class StreamletInoutlet<I, O> extends AbstractInoutlet<I, O> { protected final Streamlet<? super I, ? extends O> streamlet; public StreamletInoutlet(Streamlet<? super I, ? extends O> streamlet) { this.streamlet = streamlet; } public Streamlet<? super I, ? extends O> streamlet() { return this.streamlet; } @SuppressWarnings("unchecked") @Override public O get() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { final O output = ((GenericStreamlet<?, ? extends O>) this.streamlet).getOutput(this); if (output != null) { return output; } } if (this.input != null) { return (O) this.input.get(); } return null; } @Override protected void willInvalidate() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ? extends O>) this.streamlet).willInvalidateOutlet(this); } } @Override protected void didInvalidate() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ? extends O>) this.streamlet).didInvalidateOutlet(this); } else { this.streamlet.invalidate(); } } @Override protected void willReconcile(int version) { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ? extends O>) this.streamlet).willReconcileOutlet(this, version); } } @Override protected void didReconcile(int version) { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<? super I, ? extends O>) this.streamlet).didReconcileOutlet(this, version); } } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/StreamletOutlet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; /** * An {@code Outlet} that invalidates a parameterized {@code Streamlet} * whenever the {@code Outlet} is invalidated, and which gets its state * from the parameterized {@code Streamlet}. */ public class StreamletOutlet<O> extends AbstractOutlet<O> { protected final Streamlet<?, ? extends O> streamlet; public StreamletOutlet(Streamlet<?, ? extends O> streamlet) { this.streamlet = streamlet; } public Streamlet<?, ? extends O> streamlet() { return this.streamlet; } @Override public O get() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { return ((GenericStreamlet<?, ? extends O>) this.streamlet).getOutput(this); } return null; } @Override protected void willInvalidateInput() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<?, ? extends O>) this.streamlet).willInvalidateOutlet(this); } } @Override protected void didInvalidateInput() { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<?, ? extends O>) this.streamlet).didInvalidateOutlet(this); } else { this.streamlet.invalidate(); } } @Override protected void willReconcileInput(int version) { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<?, ? extends O>) this.streamlet).willReconcileOutlet(this, version); } } @Override protected void didReconcileInput(int version) { if (this.streamlet instanceof GenericStreamlet<?, ?>) { ((GenericStreamlet<?, ? extends O>) this.streamlet).didReconcileOutlet(this, version); } } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/StreamletScope.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public interface StreamletScope<O> { /** * Returns the lexically scoped parent of this {@code StreamletScope}. * Returns {@code null} if this {@code StreamletScope} has no lexical parent. */ StreamletScope<? extends O> streamletScope(); /** * Returns the environment in which this {@code StreamletScope} operates. */ StreamletContext streamletContext(); /** * Returns an {@code Outlet} that updates when the specified {@code key} * updates. */ Outlet<O> outlet(String key); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/ValueInput.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public class ValueInput<O> extends AbstractOutlet<O> { protected O state; public ValueInput(O state) { this.state = state; } public ValueInput() { this(null); } @Override public O get() { return this.state; } public O set(O newState) { final O oldState = this.state; this.state = newState; invalidateInput(); return oldState; } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/ValueOutput.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet; public class ValueOutput<I> extends AbstractInlet<I> { protected I state; public ValueOutput(I state) { this.state = state; } public ValueOutput() { this(null); } public I get() { return this.state; } @Override protected void onReconcileOutput(int version) { if (this.input != null) { this.state = this.input.get(); } } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Stateful streaming component model. */ package swim.streamlet;
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/FilterFieldsCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.function.FilterFieldsFunction; public class FilterFieldsCombinator<K, V, I> extends FilterFieldsOperator<K, V, I> { protected final FilterFieldsFunction<? super K, ? super V> func; public FilterFieldsCombinator(FilterFieldsFunction<? super K, ? super V> func) { this.func = func; } @Override public boolean evaluate(K key, V value) { return this.func.apply(key, value); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/FilterFieldsOperator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import java.util.Iterator; import java.util.Map; import swim.collections.HashTrieMap; import swim.streamlet.AbstractMapInletMapOutlet; import swim.streamlet.KeyEffect; import swim.util.Cursor; public abstract class FilterFieldsOperator<K, V, I> extends AbstractMapInletMapOutlet<K, K, V, V, I, Map<K, V>> { @Override public boolean containsKey(K key) { if (this.input != null) { final V value = this.input.get(key); return value != null && evaluate(key, value); } return false; } @Override public V get(K key) { if (this.input != null) { final V value = this.input.get(key); if (value != null && evaluate(key, value)) { return value; } } return null; } @Override public Map<K, V> get() { HashTrieMap<K, V> output = HashTrieMap.empty(); final Iterator<K> keys = keyIterator(); while (keys.hasNext()) { final K key = keys.next(); final V value = this.input.get(key); if (value != null && evaluate(key, value)) { output = output.updated(key, value); } } return output; } @Override public Iterator<K> keyIterator() { if (this.input != null) { return this.input.keyIterator(); } else { return Cursor.empty(); } } @Override protected void onInvalidateOutputKey(K key, KeyEffect effect) { invalidateInputKey(key, effect); } @Override protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { reconcileInputKey(key, version); } @Override protected KeyEffect willReconcileInputKey(K key, KeyEffect effect, int version) { if (effect == KeyEffect.UPDATE) { if (this.input != null) { final V value = this.input.get(key); if (value == null || !evaluate(key, value)) { return KeyEffect.REMOVE; } } } return effect; } public abstract boolean evaluate(K key, V value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/MapFieldValuesCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.function.MapFieldValuesFunction; public class MapFieldValuesCombinator<K, VI, VO, I> extends MapFieldValuesOperator<K, VI, VO, I> { protected final MapFieldValuesFunction<? super K, ? super VI, ? extends VO> func; public MapFieldValuesCombinator(MapFieldValuesFunction<? super K, ? super VI, ? extends VO> func) { this.func = func; } @Override public VO evaluate(K key, VI value) { return this.func.apply(key, value); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/MapFieldValuesOperator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import java.util.Iterator; import java.util.Map; import swim.collections.HashTrieMap; import swim.streamlet.AbstractMapInoutlet; import swim.util.Cursor; public abstract class MapFieldValuesOperator<K, VI, VO, I> extends AbstractMapInoutlet<K, VI, VO, I, Map<K, VO>> { @Override public boolean containsKey(K key) { if (this.input != null) { return this.input.containsKey(key); } else { return false; } } @Override public VO get(K key) { if (this.input != null) { return evaluate(key, this.input.get(key)); } else { return null; } } @Override public Map<K, VO> get() { HashTrieMap<K, VO> output = HashTrieMap.empty(); final Iterator<K> keys = keyIterator(); while (keys.hasNext()) { final K key = keys.next(); final VO value = evaluate(key, this.input.get(key)); if (value != null) { output = output.updated(key, value); } } return output; } @Override public Iterator<K> keyIterator() { if (this.input != null) { return this.input.keyIterator(); } else { return Cursor.empty(); } } public abstract VO evaluate(K key, VI value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/MapValueCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.function.MapValueFunction; public class MapValueCombinator<I, O> extends MapValueOperator<I, O> { protected final MapValueFunction<? super I, ? extends O> func; public MapValueCombinator(MapValueFunction<? super I, ? extends O> func) { this.func = func; } @Override public O evaluate(I value) { return this.func.apply(value); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/MapValueOperator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.AbstractInoutlet; public abstract class MapValueOperator<I, O> extends AbstractInoutlet<I, O> { @Override public O get() { if (this.input != null) { return evaluate(this.input.get()); } else { return null; } } public abstract O evaluate(I value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/MemoizeMapCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import java.util.Iterator; import swim.collections.HashTrieMap; import swim.streamlet.AbstractMapInoutlet; import swim.streamlet.KeyEffect; import swim.streamlet.MapOutlet; public class MemoizeMapCombinator<K, V, IO> extends AbstractMapInoutlet<K, V, V, IO, IO> { protected IO state; protected HashTrieMap<K, V> cache; public MemoizeMapCombinator() { this.state = null; this.cache = HashTrieMap.empty(); } @Override public boolean containsKey(K key) { return this.cache.containsKey(key); } @Override public V get(K key) { return this.cache.get(key); } @Override public IO get() { if (this.state == null && this.input != null) { this.state = this.input.get(); } return this.state; } @Override public Iterator<K> keyIterator() { return this.cache.keyIterator(); } @Override protected void onReconcileKey(K key, KeyEffect effect, int version) { if (effect == KeyEffect.UPDATE) { if (this.input != null) { final V value = this.input.get(key); if (value != null) { this.cache = this.cache.updated(key, value); } else { this.cache = this.cache.removed(key); } } } else if (effect == KeyEffect.REMOVE) { this.cache = this.cache.removed(key); } } @Override protected void onReconcile(int version) { this.state = null; } @Override public MapOutlet<K, V, IO> memoize() { return this; } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/MemoizeValueCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.AbstractInoutlet; import swim.streamlet.Outlet; public class MemoizeValueCombinator<IO> extends AbstractInoutlet<IO, IO> { protected IO state; public MemoizeValueCombinator() { this.state = null; } @Override public IO get() { return this.state; } @Override protected void onReconcile(int version) { if (this.input != null) { this.state = this.input.get(); } } @Override public Outlet<IO> memoize() { return this; } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/ReduceFieldsCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.util.CombinerFunction; public class ReduceFieldsCombinator<K, V, I, O> extends ReduceFieldsOperator<K, V, I, O> { protected final O identity; protected final CombinerFunction<? super V, O> accumulator; protected final CombinerFunction<O, O> combiner; public ReduceFieldsCombinator(O identity, CombinerFunction<? super V, O> accumulator, CombinerFunction<O, O> combiner) { this.identity = identity; this.accumulator = accumulator; this.combiner = combiner; } @Override public O get() { return this.state.reduced(this.identity, this.accumulator, this.combiner); } @Override public O identity() { return this.identity; } @Override public O accumulate(O result, V value) { return this.accumulator.combine(result, value); } @Override public O combine(O result, O value) { return this.combiner.combine(result, value); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/ReduceFieldsOperator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.collections.BTreeMap; import swim.streamlet.AbstractMapInletOutlet; import swim.streamlet.KeyEffect; public abstract class ReduceFieldsOperator<K, V, I, O> extends AbstractMapInletOutlet<K, V, I, O> { protected BTreeMap<K, V, O> state; public ReduceFieldsOperator() { this.state = new BTreeMap<K, V, O>(); } @Override public O get() { return this.state.reduced(this.identity(), this::accumulate, this::combine); } @Override protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { if (effect == KeyEffect.UPDATE) { if (this.input != null) { final V value = this.input.get(key); if (value != null) { this.state.put(key, value); } else { this.state.remove(key); } } } else if (effect == KeyEffect.REMOVE) { this.state.remove(key); } } public abstract O identity(); public abstract O accumulate(O result, V value); public abstract O combine(O result, O value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/WatchFieldsCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.function.WatchFieldsFunction; public class WatchFieldsCombinator<K, V, O> extends WatchFieldsOperator<K, V, O> { protected final WatchFieldsFunction<? super K, ? super V> func; public WatchFieldsCombinator(WatchFieldsFunction<? super K, ? super V> func) { this.func = func; } @Override public void evaluate(K key, V value) { this.func.apply(key, value); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/WatchFieldsOperator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.AbstractMapInlet; import swim.streamlet.KeyEffect; public abstract class WatchFieldsOperator<K, V, O> extends AbstractMapInlet<K, V, O> { @Override protected void onReconcileOutputKey(K key, KeyEffect effect, int version) { if (effect == KeyEffect.UPDATE) { if (this.input != null) { evaluate(key, this.input.get(key)); } else { evaluate(key, null); } } else if (effect == KeyEffect.REMOVE) { evaluate(key, null); } } public abstract void evaluate(K key, V value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/WatchValueCombinator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.function.WatchValueFunction; public class WatchValueCombinator<I> extends WatchValueOperator<I> { protected final WatchValueFunction<? super I> func; public WatchValueCombinator(WatchValueFunction<? super I> func) { this.func = func; } @Override public void evaluate(I value) { this.func.apply(value); } }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/WatchValueOperator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.combinator; import swim.streamlet.AbstractInlet; public abstract class WatchValueOperator<I> extends AbstractInlet<I> { @Override protected void onReconcileOutput(int version) { if (this.input != null) { evaluate(this.input.get()); } } public abstract void evaluate(I input); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/combinator/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Stateful streaming operators and combinators. */ package swim.streamlet.combinator;
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/function/FilterFieldsFunction.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.function; @FunctionalInterface public interface FilterFieldsFunction<K, V> { boolean apply(K key, V value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/function/MapFieldValuesFunction.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.function; @FunctionalInterface public interface MapFieldValuesFunction<K, VI, VO> { VO apply(K key, VI value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/function/MapValueFunction.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.function; @FunctionalInterface public interface MapValueFunction<I, O> { O apply(I value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/function/WatchFieldsFunction.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.function; @FunctionalInterface public interface WatchFieldsFunction<K, V> { void apply(K key, V value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/function/WatchValueFunction.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.streamlet.function; @FunctionalInterface public interface WatchValueFunction<I> { void apply(I value); }
0
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet
java-sources/ai/swim/swim-streamlet/3.10.0/swim/streamlet/function/package-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Stateful streaming combinator function interfaces. */ package swim.streamlet.function;
0
java-sources/ai/swim/swim-structure
java-sources/ai/swim/swim-structure/3.10.0/module-info.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * Uniform structured data model. */ module swim.structure { requires swim.util; requires transitive swim.codec; requires transitive swim.collections; exports swim.structure; exports swim.structure.collections; exports swim.structure.form; exports swim.structure.func; exports swim.structure.operator; exports swim.structure.selector; }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Absent.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.Iterator; import swim.codec.Output; import swim.util.Murmur3; public final class Absent extends Value { private Absent() { // stub } /** * Always returns {@code false} because {@code Absent} represents an * undefined value. */ @Override public boolean isDefined() { return false; } @Override public boolean isConstant() { return true; } /** * Always returns {@code false} because {@code Absent} is not a distinct * value. */ @Override public boolean isDistinct() { return false; } /** * Always returns an empty {@code Record} because {@code Absent} is not a * distinct value. */ @Override public Record unflattened() { return Record.empty(); } @Override public Record updated(Value key, Value value) { return Record.of(Slot.of(key, value)); } @Override public Record updatedAttr(Text key, Value value) { return Record.of(Attr.of(key, value)); } @Override public Record updatedSlot(Value key, Value value) { return Record.of(Slot.of(key, value)); } @Override public Record appended(Item item) { return Record.of(item); } @Override public Record appended(Object... items) { return Record.of(items); } @Override public Record prepended(Item item) { return Record.of(item); } @Override public Record prepended(Object... items) { return Record.of(items); } @Override public Record concat(Item that) { if (!that.isDefined()) { return Record.create(); } else if (that instanceof Record) { return ((Record) that).branch(); } else { return Record.of(that); } } @Override public Item conditional(Item thenTerm, Item elseTerm) { return elseTerm; } @Override public Value conditional(Value thenTerm, Value elseTerm) { return elseTerm; } @Override public Item or(Item that) { return that; } @Override public Value or(Value that) { return that; } @Override public Item and(Item that) { return this; } @Override public Value and(Value that) { return this; } @Override public Value not() { return Value.extant(); } /** * Always returns {@code false} because {@code Absent} behaves like a falsey * value. */ @Override public boolean booleanValue() { return false; } /** * Always returns {@code false} because {@code Absent} behaves like a falsey * value. */ @Override public boolean booleanValue(boolean orElse) { return false; } @Override public Iterator<Item> iterator() { return new ItemIterator(null); } @Override public int typeOrder() { return 99; } @Override public int compareTo(Item other) { return Integer.compare(typeOrder(), other.typeOrder()); } @Override public boolean equals(Object other) { return this == other; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(Absent.class); } return hashSeed; } @Override public void debug(Output<?> output) { output = output.write("Value").write('.').write("absent").write('(').write(')'); } private static int hashSeed; private static final Absent VALUE = new Absent(); public static Absent absent() { return VALUE; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Attr.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; import swim.structure.operator.BitwiseAndOperator; import swim.structure.operator.BitwiseOrOperator; import swim.structure.operator.BitwiseXorOperator; import swim.structure.operator.DivideOperator; import swim.structure.operator.MinusOperator; import swim.structure.operator.ModuloOperator; import swim.structure.operator.PlusOperator; import swim.structure.operator.TimesOperator; public final class Attr extends Field { final Text key; Value value; Attr(Text key, Value value, int flags) { this.key = key; this.value = value; this.flags = flags; } Attr(Text key, Value value) { this(key, value, 0); } @Override public boolean isConstant() { return this.key.isConstant() && this.value.isConstant(); } public String name() { return this.key.value; } @Override public Text key() { return this.key; } @Override public Value value() { return this.value; } @Override public Value setValue(Value newValue) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } final Value oldValue = this.value; this.value = newValue; return oldValue; } @Override public Attr updatedValue(Value value) { if (value == null) { throw new NullPointerException(); } return new Attr(key, value); } @Override public Item bitwiseOr(Item that) { if (that instanceof Expression) { return new BitwiseOrOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.bitwiseOr(((Attr) that).value); } else if (that instanceof Value) { newValue = value.bitwiseOr((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item bitwiseXor(Item that) { if (that instanceof Expression) { return new BitwiseXorOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.bitwiseXor(((Attr) that).value); } else if (that instanceof Value) { newValue = value.bitwiseXor((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item bitwiseAnd(Item that) { if (that instanceof Expression) { return new BitwiseAndOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.bitwiseAnd(((Attr) that).value); } else if (that instanceof Value) { newValue = value.bitwiseAnd((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item plus(Item that) { if (that instanceof Expression) { return new PlusOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.plus(((Attr) that).value); } else if (that instanceof Value) { newValue = value.plus((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item minus(Item that) { if (that instanceof Expression) { return new MinusOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.minus(((Attr) that).value); } else if (that instanceof Value) { newValue = value.minus((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item times(Item that) { if (that instanceof Expression) { return new TimesOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.times(((Attr) that).value); } else if (that instanceof Value) { newValue = value.times((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item divide(Item that) { if (that instanceof Expression) { return new DivideOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.divide(((Attr) that).value); } else if (that instanceof Value) { newValue = value.divide((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item modulo(Item that) { if (that instanceof Expression) { return new ModuloOperator(this, that); } final Value newValue; if (that instanceof Attr && key.equals(((Attr) that).key)) { newValue = value.modulo(((Attr) that).value); } else if (that instanceof Value) { newValue = value.modulo((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item not() { final Value newValue = value.not(); if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item bitwiseNot() { final Value newValue = value.bitwiseNot(); if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item negative() { final Value newValue = value.negative(); if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item positive() { final Value newValue = value.positive(); if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item inverse() { final Value newValue = value.inverse(); if (newValue.isDefined()) { return new Attr(key, newValue); } return Item.absent(); } @Override public Item evaluate(Interpreter interpreter) { final Value key = this.key.evaluate(interpreter).toValue(); final Value value = this.value.evaluate(interpreter).toValue(); if (key == this.key && value == this.value) { return this; } else if (key.isDefined() && value.isDefined()) { if (key instanceof Text) { return new Attr((Text) key, value); } else { return new Slot(key, value); } } return Item.absent(); } @Override public Item substitute(Interpreter interpreter) { final Value key = this.key.substitute(interpreter).toValue(); final Value value = this.value.substitute(interpreter).toValue(); if (key == this.key && value == this.value) { return this; } else if (key.isDefined() && value.isDefined()) { if (key instanceof Text) { return new Attr((Text) key, value); } else { return new Slot(key, value); } } return Item.absent(); } @Override public boolean isAliased() { return false; } @Override public boolean isMutable() { return (this.flags & IMMUTABLE) == 0; } @Override public void alias() { do { final int oldFlags = this.flags; if ((oldFlags & IMMUTABLE) == 0) { final int newFlags = oldFlags | IMMUTABLE; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } else { break; } } while (true); } @Override public Attr branch() { if ((this.flags & IMMUTABLE) != 0) { return new Attr(key, value, flags & ~IMMUTABLE); } else { return this; } } @Override public Attr commit() { do { final int oldFlags = this.flags; if ((oldFlags & IMMUTABLE) == 0) { final int newFlags = oldFlags | IMMUTABLE; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { this.value.commit(); break; } } else { break; } } while (true); return this; } @Override public int typeOrder() { return 1; } @Override public int compareTo(Item other) { if (other instanceof Attr) { return compareTo((Attr) other); } return Integer.compare(typeOrder(), other.typeOrder()); } int compareTo(Attr that) { int order = this.key.compareTo(that.key); if (order == 0) { order = this.value.compareTo(that.value); } return order; } @Override public boolean keyEquals(Object key) { if (key instanceof String) { return this.key.value.equals(key); } else if (key instanceof Field) { return this.key.equals(((Field) key).getKey()); } else { return this.key.equals(key); } } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof Attr) { final Attr that = (Attr) other; return this.key.equals(that.key) && this.value.equals(that.value); } return false; } @Override public int hashCode() { return this.key.hashCode() ^ this.value.hashCode(); } @Override public void debug(Output<?> output) { output = output.write("Attr").write('.').write("of").write('(').display(this.key); if (!(this.value instanceof Extant)) { output = output.write(", ").display(this.value); } output = output.write(')'); } public static Attr of(Text key, Value value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Attr(key, value); } public static Attr of(Text key, String value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Attr(key, Text.from(value)); } public static Attr of(Text key, int value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(key, Num.from(value)); } public static Attr of(Text key, long value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(key, Num.from(value)); } public static Attr of(Text key, float value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(key, Num.from(value)); } public static Attr of(Text key, double value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(key, Num.from(value)); } public static Attr of(Text key, boolean value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(key, Bool.from(value)); } public static Attr of(String key, Value value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Attr(Text.from(key), value); } public static Attr of(String key, String value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Attr(Text.from(key), Text.from(value)); } public static Attr of(String key, int value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(Text.from(key), Num.from(value)); } public static Attr of(String key, long value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(Text.from(key), Num.from(value)); } public static Attr of(String key, float value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(Text.from(key), Num.from(value)); } public static Attr of(String key, double value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(Text.from(key), Num.from(value)); } public static Attr of(String key, boolean value) { if (key == null) { throw new NullPointerException("key"); } return new Attr(Text.from(key), Bool.from(value)); } public static Attr of(Text key) { if (key == null) { throw new NullPointerException("key"); } return new Attr(key, Value.extant()); } public static Attr of(String key) { if (key == null) { throw new NullPointerException("key"); } return new Attr(Text.from(key), Value.extant()); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Bool.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; public final class Bool extends Value { final boolean value; private Bool(boolean value) { this.value = value; } @Override public boolean isConstant() { return true; } @Override public String stringValue() { return this.value ? "true" : "false"; } @Override public boolean booleanValue() { return this.value; } @Override public boolean booleanValue(boolean orElse) { return this.value; } @Override public Item conditional(Item thenTerm, Item elseTerm) { return this.value ? thenTerm : elseTerm; } public Value conditional(Value thenTerm, Value elseTerm) { return this.value ? thenTerm : elseTerm; } @Override public Item or(Item that) { return this.value ? this : that; } public Value or(Value that) { return this.value ? this : that; } @Override public Item and(Item that) { return this.value ? that : this; } public Value and(Value that) { return this.value ? that : this; } @Override public Value not() { return Bool.from(!this.value); } @Override public int typeOrder() { return 7; } @Override public int compareTo(Item other) { if (other instanceof Bool) { return compareTo((Bool) other); } return Integer.compare(typeOrder(), other.typeOrder()); } int compareTo(Bool that) { if (this.value && !that.value) { return -1; } else if (!this.value && that.value) { return 1; } else { return 0; } } @Override public boolean equals(Object other) { return this == other; } @Override public int hashCode() { return Boolean.valueOf(this.value).hashCode(); } @Override public void debug(Output<?> output) { output = output.write("Bool").write('.').write("from") .write('(').write(this.value ? "true" : "false").write(')'); } @Override public void display(Output<?> output) { output = output.write(this.value ? "true" : "false"); } private static final Bool TRUE = new Bool(true); private static final Bool FALSE = new Bool(false); public static Bool from(boolean value) { return value ? TRUE : FALSE; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Data.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.nio.ByteBuffer; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import swim.codec.Base16; import swim.codec.Base64; import swim.codec.Binary; import swim.codec.InputBuffer; import swim.codec.Output; import swim.codec.OutputSettings; import swim.codec.Unicode; import swim.codec.Utf8; import swim.codec.Writer; import swim.util.Murmur3; public class Data extends Value { byte[] array; int offset; int size; volatile int flags; Data(byte[] array, int offset, int size, int flags) { this.array = array; this.offset = offset; this.size = size; this.flags = flags; } protected Data(byte[] array, int offset, int size) { this.array = array; this.offset = offset; this.size = size; this.flags = 0; } public Data(int initialCapacity) { if (initialCapacity < 0) { throw new IllegalArgumentException(Integer.toString(initialCapacity)); } this.array = new byte[initialCapacity]; this.offset = 0; this.size = 0; this.flags = 0; } public Data() { this.array = null; this.offset = 0; this.size = 0; this.flags = ALIASED; } @Override public boolean isConstant() { return true; } public final int size() { return this.size; } public byte getByte(int index) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } return this.array[this.offset + index]; } public Data setByte(int index, byte value) { final int flags = this.flags; if ((flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index >= this.size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((flags & ALIASED) != 0) { return setByteAliased(index, value); } else { return setByteMutable(index, value); } } private Data setByteAliased(int index, byte value) { final int n = this.size; final byte[] oldArray = this.array; final byte[] newArray = new byte[expand(n)]; System.arraycopy(oldArray, this.offset, newArray, 0, n); newArray[index] = value; this.array = newArray; this.offset = 0; do { final int oldFlags = this.flags; final int newFlags = oldFlags & ~ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } while (true); return this; } private Data setByteMutable(int index, byte value) { this.array[this.offset + index] = value; return this; } public Data addByte(byte value) { final int flags = this.flags; if ((flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((flags & ALIASED) != 0) { return addByteAliased(value); } else { return addByteMutable(value); } } private Data addByteAliased(byte value) { final int n = this.size; final byte[] oldArray = this.array; final byte[] newArray = new byte[expand(n + 1)]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } newArray[n] = value; this.array = newArray; this.offset = 0; this.size = n + 1; do { final int oldFlags = this.flags; final int newFlags = oldFlags & ~ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } while (true); return this; } private Data addByteMutable(byte value) { final int n = this.size; final byte[] oldArray = this.array; final byte[] newArray; if (oldArray == null || n + 1 > oldArray.length) { newArray = new byte[expand(n + 1)]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } this.array = newArray; this.offset = 0; } else { newArray = oldArray; } newArray[this.offset + n] = value; this.size = n + 1; return this; } public Data addByteArray(byte[] array, int offset, int size) { final int flags = this.flags; if ((flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((flags & ALIASED) != 0) { return addByteArrayAliased(array, offset, size); } else { return addByteArrayMutable(array, offset, size); } } public Data addByteArray(byte[] array) { return addByteArray(array, 0, array.length); } private Data addByteArrayAliased(byte[] array, int offset, int size) { if (size == 0) { return this; } final int n = this.size; final byte[] oldArray = this.array; final byte[] newArray = new byte[expand(n + size)]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } System.arraycopy(array, offset, newArray, n, size); this.array = newArray; this.offset = 0; this.size = n + size; do { final int oldFlags = this.flags; final int newFlags = oldFlags & ~ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } while (true); return this; } private Data addByteArrayMutable(byte[] array, int offset, int size) { if (size == 0) { return this; } final int n = this.size; final byte[] oldArray = this.array; final byte[] newArray; if (oldArray == null || n + size > oldArray.length) { newArray = new byte[expand(n + size)]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } this.array = newArray; this.offset = 0; } else { newArray = oldArray; } System.arraycopy(array, offset, newArray, this.offset + n, size); this.size = n + size; return this; } public Data addData(Data data) { return addByteArray(data.array, data.offset, data.size); } public void clear() { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } this.array = null; this.offset = 0; this.size = 0; this.flags = Data.ALIASED; } public byte[] toByteArray() { final int n = this.size; final byte[] oldArray = this.array; final int flags = this.flags; if ((flags & IMMUTABLE) != 0) { final byte[] newArray = new byte[n]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } return newArray; } else if ((flags & ALIASED) != 0 || n != oldArray.length) { final byte[] newArray = new byte[n]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } this.array = newArray; this.offset = 0; do { final int oldFlags = this.flags; final int newFlags = oldFlags & ~ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } while (true); return newArray; } else { return oldArray; } } public byte[] asByteArray() { return this.array; } public ByteBuffer toByteBuffer() { final int flags = this.flags; if ((flags & ALIASED) != 0) { final int n = this.size; final byte[] oldArray = this.array; final byte[] newArray = new byte[n]; if (oldArray != null) { System.arraycopy(oldArray, this.offset, newArray, 0, n); } return ByteBuffer.wrap(newArray); } else { ByteBuffer buffer = ByteBuffer.wrap(this.array, this.offset, this.size); if ((flags & IMMUTABLE) != 0) { buffer = buffer.asReadOnlyBuffer(); } do { final int oldFlags = FLAGS.get(this); final int newFlags = oldFlags | ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } while (true); return buffer; } } public ByteBuffer asByteBuffer() { if (this.array != null && this.size > 0) { return ByteBuffer.wrap(this.array, this.offset, this.size); } else { return null; } } public InputBuffer toInputBuffer() { return Binary.inputBuffer(toByteArray()); } @Override public boolean isAliased() { return (this.flags & ALIASED) != 0; } @Override public boolean isMutable() { return (this.flags & IMMUTABLE) == 0; } @Override public Data branch() { do { final int oldFlags = this.flags; if ((oldFlags & ALIASED) == 0) { final int newFlags = oldFlags | ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } else { break; } } while (true); return new Data(this.array, this.offset, this.size, ALIASED); } @Override public Data commit() { do { final int oldFlags = this.flags; if ((oldFlags & IMMUTABLE) == 0) { final int newFlags = oldFlags | IMMUTABLE; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } else { break; } } while (true); return this; } public Writer<?, ?> writer() { if (this.array != null && this.size > 0) { final ByteBuffer buffer = ByteBuffer.wrap(this.array, 0, this.size); do { final int oldFlags = FLAGS.get(this); if ((oldFlags & ALIASED) == 0) { final int newFlags = oldFlags | ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } else { break; } } while (true); return Binary.byteBufferWriter(buffer); } else { return Writer.done(); } } public Writer<?, ?> write(Output<?> output) { if (this.array != null && this.size > 0) { final ByteBuffer buffer = ByteBuffer.wrap(this.array, 0, this.size); do { final int oldFlags = FLAGS.get(this); if ((oldFlags & ALIASED) == 0) { final int newFlags = oldFlags | ALIASED; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } else { break; } } while (true); return Binary.writeByteBuffer(buffer, output); } else { return Writer.done(); } } public Writer<?, ?> writeBase16(Output<?> output, Base16 base16) { if (this.array != null && this.size != 0) { return base16.writeByteBuffer(ByteBuffer.wrap(this.array, this.offset, this.size), output); } else { return Writer.done(); } } public Writer<?, ?> writeBase16(Output<?> output) { return writeBase16(output, Base16.uppercase()); } public String toBase16(Base16 base16) { final Output<String> output = Unicode.stringOutput(); writeBase16(output, base16); return output.bind(); } public String toBase16() { return toBase16(Base16.uppercase()); } public Writer<?, ?> writeBase64(Output<?> output, Base64 base64) { if (this.array != null && this.size != 0) { return base64.writeByteBuffer(ByteBuffer.wrap(this.array, this.offset, this.size), output); } else { return Writer.done(); } } public Writer<?, ?> writeBase64(Output<?> output) { return writeBase64(output, Base64.standard()); } public String toBase64(Base64 base64) { final Output<String> output = Unicode.stringOutput(); writeBase64(output, base64); return output.bind(); } public String toBase64() { return toBase64(Base64.standard()); } @Override public int typeOrder() { return 4; } @Override public int compareTo(Item other) { if (other instanceof Data) { return compareTo((Data) other); } return Integer.compare(typeOrder(), other.typeOrder()); } int compareTo(Data that) { final byte[] xs = this.array; final byte[] ys = that.array; int xi = this.offset; int yi = that.offset; final int xn = this.size; final int yn = that.size; final int xu = xi + xn; final int ju = yi + yn; int order = 0; do { if (xi < xu && yi < ju) { order = xs[xi] - ys[yi]; xi += 1; yi += 1; } else { break; } } while (order == 0); if (order > 0) { return 1; } else if (order < 0) { return -1; } else if (xn > yn) { return 1; } else if (xn < yn) { return -1; } else { return 0; } } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof Data) { final Data that = (Data) other; final byte[] xs = this.array; final byte[] ys = that.array; int xi = this.offset; int yi = that.offset; final int xn = this.size; if (xn != that.size) { return false; } final int xu = xi + xn; while (xi < xu) { if (xs[xi] != ys[yi]) { return false; } xi += 1; yi += 1; } return true; } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(Data.class); } return Murmur3.mash(Murmur3.mix(hashSeed, this.array, this.offset, this.size)); } @Override public void debug(Output<?> output) { output = output.write("Data").write('.'); if (this.size == 0) { output = output.write("empty").write('(').write(')'); } else { output = output.write("fromBase16").write('(').write('"'); writeBase16(output); output = output.write('"').write(')'); } } static final int ALIASED = 1 << 0; static final int IMMUTABLE = 1 << 1; static final AtomicIntegerFieldUpdater<Data> FLAGS = AtomicIntegerFieldUpdater.newUpdater(Data.class, "flags"); private static Data empty; private static int hashSeed; public static Output<Data> output(Data data) { return new DataOutput(data, OutputSettings.standard()); } public static Output<Data> output(int initialCapacity) { return new DataOutput(new Data(initialCapacity), OutputSettings.standard()); } public static Output<Data> output() { return new DataOutput(new Data(), OutputSettings.standard()); } public static Data empty() { if (empty == null) { empty = new Data(null, 0, 0, ALIASED | IMMUTABLE); } return empty; } public static Data create() { return new Data(null, 0, 0, ALIASED); } public static Data create(int initialCapacity) { return new Data(new byte[initialCapacity], 0, 0, 0); } public static Data wrap(ByteBuffer buffer) { if (!buffer.hasArray()) { throw new IllegalArgumentException(); } return new Data(buffer.array(), buffer.arrayOffset(), buffer.remaining(), ALIASED); } public static Data wrap(byte[] array, int offset, int size) { return new Data(array, offset, size, ALIASED); } public static Data wrap(byte[] array) { return new Data(array, 0, array.length, ALIASED); } public static Data from(ByteBuffer buffer) { final int n = buffer.remaining(); if (buffer.hasArray()) { final byte[] array = buffer.array(); return new Data(array, buffer.arrayOffset(), buffer.remaining(), ALIASED); } else { final byte[] array = new byte[n]; buffer.get(array); return new Data(array, 0, n, 0); } } public static Data fromBase16(String string) { return Base16.parse(Unicode.stringInput(string), output()).bind(); } public static Data fromBase64(String string, Base64 base64) { return base64.parse(Unicode.stringInput(string), output()).bind(); } public static Data fromBase64(String string) { return fromBase64(string, Base64.standard()); } public static Data fromUtf8(String string) { Output<Data> output = Utf8.encodedOutput(output()); int i = 0; final int n = string.length(); while (i < n) { output = output.write(string.codePointAt(i)); i = string.offsetByCodePoints(i, 1); } return output.bind(); } static int expand(int n) { n = Math.max(32, n) - 1; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n + 1; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/DataOutput.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; import swim.codec.OutputSettings; final class DataOutput extends Output<Data> { final Data data; OutputSettings settings; DataOutput(Data data, OutputSettings settings) { this.data = data; this.settings = settings; } @Override public boolean isCont() { return true; } @Override public boolean isFull() { return false; } @Override public boolean isDone() { return false; } @Override public boolean isError() { return false; } @Override public boolean isPart() { return false; } @Override public Output<Data> isPart(boolean isPart) { return this; } @Override public Output<Data> write(int b) { this.data.addByte((byte) b); return this; } @Override public Output<Data> write(String string) { throw new UnsupportedOperationException(); } @Override public Output<Data> writeln(String string) { throw new UnsupportedOperationException(); } @Override public Output<Data> writeln() { throw new UnsupportedOperationException(); } @Override public OutputSettings settings() { return this.settings; } @Override public Output<Data> settings(OutputSettings settings) { this.settings = settings; return this; } @Override public Data bind() { return this.data; } @Override public Output<Data> clone() { return new DataOutput(this.data.branch(), this.settings); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Expression.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.structure.operator.AndOperator; import swim.structure.operator.BitwiseAndOperator; import swim.structure.operator.BitwiseNotOperator; import swim.structure.operator.BitwiseOrOperator; import swim.structure.operator.BitwiseXorOperator; import swim.structure.operator.ConditionalOperator; import swim.structure.operator.DivideOperator; import swim.structure.operator.EqOperator; import swim.structure.operator.GeOperator; import swim.structure.operator.GtOperator; import swim.structure.operator.LeOperator; import swim.structure.operator.LtOperator; import swim.structure.operator.MinusOperator; import swim.structure.operator.ModuloOperator; import swim.structure.operator.NeOperator; import swim.structure.operator.NegativeOperator; import swim.structure.operator.NotOperator; import swim.structure.operator.OrOperator; import swim.structure.operator.PlusOperator; import swim.structure.operator.PositiveOperator; import swim.structure.operator.TimesOperator; /** * A combination of operators, constants, and variables. Every {@code Item} * in the Swim data model can be {@link #evaluate(Interpreter) evaluated} * against a scope. An {@code Expression} is some {@code Value} that, when * evaluated, may yield a different value than the {@code Expression} itself. * Note that this is a stricter definition than that of a logical expression; * for example, the number {@code 2} is a valid expression, but it is not an * {@code Expression}. * <p> * An {@code Expression} can be either a {@link Selector} or an {@link * Operator}. A {@code Selector} references specific attributes of a Swim model * instance. An {@code Operator} identifies an operation on constants, * variables, or {@code Selector} expressions. Together, these form a foundation * for building expression languages that can both manipulate and read Swim * objects. */ public abstract class Expression extends Value { @Override public Item conditional(Item thenTerm, Item elseTerm) { return new ConditionalOperator(this, thenTerm, elseTerm); } @Override public Value conditional(Value thenTerm, Value elseTerm) { return new ConditionalOperator(this, thenTerm, elseTerm); } @Override public Operator or(Item that) { return new OrOperator(this, that); } @Override public Operator or(Value that) { return new OrOperator(this, that); } @Override public Operator and(Item that) { return new AndOperator(this, that); } @Override public Operator and(Value that) { return new AndOperator(this, that); } @Override public Operator bitwiseOr(Item that) { return new BitwiseOrOperator(this, that); } @Override public Operator bitwiseOr(Value that) { return new BitwiseOrOperator(this, that); } @Override public Operator bitwiseXor(Item that) { return new BitwiseXorOperator(this, that); } @Override public Operator bitwiseXor(Value that) { return new BitwiseXorOperator(this, that); } @Override public Operator bitwiseAnd(Item that) { return new BitwiseAndOperator(this, that); } @Override public Operator bitwiseAnd(Value that) { return new BitwiseAndOperator(this, that); } @Override public Operator lt(Item that) { return new LtOperator(this, that); } @Override public Operator lt(Value that) { return new LtOperator(this, that); } @Override public Operator le(Item that) { return new LeOperator(this, that); } @Override public Operator le(Value that) { return new LeOperator(this, that); } @Override public Operator eq(Item that) { return new EqOperator(this, that); } @Override public Operator eq(Value that) { return new EqOperator(this, that); } @Override public Operator ne(Item that) { return new NeOperator(this, that); } @Override public Operator ne(Value that) { return new NeOperator(this, that); } @Override public Operator ge(Item that) { return new GeOperator(this, that); } @Override public Operator ge(Value that) { return new GeOperator(this, that); } @Override public Operator gt(Item that) { return new GtOperator(this, that); } @Override public Operator gt(Value that) { return new GtOperator(this, that); } @Override public Operator plus(Item that) { return new PlusOperator(this, that); } @Override public Operator plus(Value that) { return new PlusOperator(this, that); } @Override public Operator minus(Item that) { return new MinusOperator(this, that); } @Override public Operator minus(Value that) { return new MinusOperator(this, that); } @Override public Operator times(Item that) { return new TimesOperator(this, that); } @Override public Operator times(Value that) { return new TimesOperator(this, that); } @Override public Operator divide(Item that) { return new DivideOperator(this, that); } @Override public Operator divide(Value that) { return new DivideOperator(this, that); } @Override public Operator modulo(Item that) { return new ModuloOperator(this, that); } @Override public Operator modulo(Value that) { return new ModuloOperator(this, that); } @Override public Operator not() { return new NotOperator(this); } @Override public Operator bitwiseNot() { return new BitwiseNotOperator(this); } @Override public Operator negative() { return new NegativeOperator(this); } @Override public Operator positive() { return new PositiveOperator(this); } @Override public Operator inverse() { return new DivideOperator(Num.from(1.0), this); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Extant.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; import swim.util.Murmur3; public final class Extant extends Value { private Extant() { // stub } /** * Always returns {@code true} because {@code Extant} is a defined value. */ @Override public boolean isDefined() { return true; } /** * Always returns {@code false} because {@code Extant} is not a distinct * value. */ @Override public boolean isDistinct() { return false; } @Override public boolean isConstant() { return true; } /** * Always returns an empty {@code Record} because {@code Extant} is not a * distinct value. */ @Override public Record unflattened() { return Record.empty(); } @Override public Value not() { return Value.absent(); } /** * Always returns the empty {@code String} because {@code Extant} behaves * like an empty {@code Record}, which converts to a {@code String} by * concatenating the string values of all its members, if all its members * convert to string values. */ @Override public String stringValue() { return ""; } /** * Always returns the empty {@code String} because {@code Extant} behaves * like an empty {@code Record}, which converts to a {@code String} by * concatenating the string values of all its members, if all its members * convert to string values. */ @Override public String stringValue(String orElse) { return ""; } /** * Always returns {@code true} because {@code Extant} behaves like a truthy * value. */ @Override public boolean booleanValue() { return true; } /** * Always returns {@code true} because {@code Extant} behaves like a truthy * value. */ @Override public boolean booleanValue(boolean orElse) { return true; } @Override public int typeOrder() { return 98; } @Override public int compareTo(Item other) { return Integer.compare(typeOrder(), other.typeOrder()); } @Override public boolean equals(Object other) { return this == other; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(Extant.class); } return hashSeed; } @Override public void debug(Output<?> output) { output = output.write("Value").write('.').write("extant").write('(').write(')'); } private static int hashSeed; private static final Extant VALUE = new Extant(); public static Extant extant() { return VALUE; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Field.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import java.util.Map; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; public abstract class Field extends Item implements Map.Entry<Value, Value> { volatile int flags; Field() { // stub } /** * Always returns {@code true} because a {@code Field} can never be * {@link Absent}. */ @Override public final boolean isDefined() { return true; } /** * Always returns {@code true} because a {@code Field} can be neither * {@link Extant} nor {@link Absent}. */ @Override public final boolean isDistinct() { return true; } /** * Returns the key component of this {@code Field}. */ @Override public abstract Value key(); /** * Returns the key component of this {@code Field}. Equivalent to {@link * #key()}. */ @Override public Value getKey() { return key(); } /** * Returns the value component of this {@code Field}. */ public abstract Value value(); /** * Returns the value component of this {@code Field}. Equivalent to {@link * #value()}. */ @Override public Value getValue() { return value(); } /** * Sets the value of this {@code Field} to the new {@code value}, returning * the old value. * * @throws UnsupportedOperationException if this {@code Field} is immutable. */ @Override public abstract Value setValue(Value value); /** * Returns a copy of this {@code Field} with the updated {@code value}. */ public abstract Field updatedValue(Value value); /** * Returns the value component of this {@code Field}. Equivalent to {@link * #value()}. */ @Override public Value toValue() { return value(); } /** * Always returns {@code null} because a {@code Field} can't be a {@code * Record}, so it can't have a first member {@code Attr} whose key string * could be returned. */ @Override public final String tag() { return null; } /** * Always returns the value component of this {@code Field}. */ @Override public final Value target() { return value(); } /** * Always returns {@link Absent} because a {@code Field} can't be flattened * into a {@code Value}. */ @Override public final Value flattened() { return Value.absent(); } /** * Returns a {@code Record} containing just this {@code Field}. */ @Override public final Record unflattened() { return Record.of(this); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a head {@code Attr} whose value could be * returned if its key were equal to the {@code tag}. */ @Override public final Value header(String tag) { return Value.absent(); } /** * Always returns {@code null} because a {@code Field} can't be a {@code * Record}, so it can't have a head {@code Attr} whose value could be * returned as a {@code Record} if its key is equal to the {@code tag}. */ @Override public final Record headers(String tag) { return null; } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a first member. */ @Override public final Item head() { return Item.absent(); } /** * Always returns an empty {@code Record} because a {@code Field} can't * itself be a {@code Record}, so it can't have any non-first members. */ @Override public final Record tail() { return Record.empty(); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have any non-first members to flatten, and because a * {@code Field} isn't a distinct {@code Value}, so it can't return {@code * Extant}. */ @Override public final Value body() { return Value.absent(); } /** * Always returns {@code 0} because a {@code Field} can't be a {@code Record}, * so it can't contain any members. */ @Override public final int length() { return 0; } /** * Always returns {@code false} because a {@code Field} can't be a {@code * Record}, so it can't have a member equal to {@code item}. */ @Override public final boolean contains(Item item) { return false; } /** * Always returns {@code false} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose key is equal to the * given {@code key}. */ @Override public final boolean containsKey(Value key) { return false; } /** * Always returns {@code false} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose key string is equal * to the given {@code key}. */ @Override public final boolean containsKey(String key) { return false; } /** * Always returns {@code false} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose value is equal to * the given {@code value}. */ @Override public final boolean containsValue(Value value) { return false; } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose key is equal to the * given {@code key}. */ @Override public final Value get(Value key) { return Value.absent(); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose key string is equal * to the given {@code key}. */ @Override public final Value get(String key) { return Value.absent(); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have an {@code Attr} member whose key is equal to the * given {@code key}. */ @Override public final Value getAttr(Text key) { return Value.absent(); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have an {@code Attr} member whose key string is equal * to the given {@code key}. */ @Override public final Value getAttr(String key) { return Value.absent(); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Slot} member whose key is equal to the * given {@code key}. */ @Override public final Value getSlot(Value key) { return Value.absent(); } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Slot} member whose key string is equal * to the given {@code key}. */ @Override public final Value getSlot(String key) { return Value.absent(); } /** * Always returns {@code null} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose key is equal to the * given {@code key}. */ @Override public final Field getField(Value key) { return null; } /** * Always returns {@code null} because a {@code Field} can't be a {@code * Record}, so it can't have a {@code Field} member whose key string is equal * to the given {@code key}. */ @Override public final Field getField(String key) { return null; } /** * Always returns {@link Absent} because a {@code Field} can't be a {@code * Record}, so it can't have a member at the given {@code index}. */ @Override public final Item getItem(int index) { return Item.absent(); } @Override public Field removed(Value key) { return this; } @Override public Field removed(String key) { return this; } @Override public Item conditional(Item thenTerm, Item elseTerm) { if (thenTerm instanceof Field && elseTerm instanceof Field) { return conditional((Field) thenTerm, (Field) elseTerm); } return thenTerm; } public Field conditional(Field thenTerm, Field elseTerm) { return thenTerm; } @Override public Item or(Item that) { if (that instanceof Field) { return or((Field) that); } return this; } public Field or(Field that) { return this; } @Override public Item and(Item that) { if (that instanceof Field) { return and((Field) that); } return that; } public Field and(Field that) { return that; } @Override public Value lambda(Value template) { return Value.absent(); } /** * Converts the value of this {@code Field} into a {@code String} value, * if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a {@code String} value. */ @Override public final String stringValue() { return getValue().stringValue(); } /** * Converts the value of this {@code Field} into a {@code String} value, * if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a {@code string} value. */ @Override public final String stringValue(String orElse) { return getValue().stringValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code byte} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code byte} value. */ @Override public final byte byteValue() { return getValue().byteValue(); } /** * Converts the value of this {@code Field} into a primitive {@code byte} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code byte} value. */ @Override public final byte byteValue(byte orElse) { return getValue().byteValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code short} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code short} value. */ @Override public final short shortValue() { return getValue().shortValue(); } /** * Converts the value of this {@code Field} into a primitive {@code short} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code short} value. */ @Override public final short shortValue(short orElse) { return getValue().shortValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code int} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code int} value. */ @Override public final int intValue() { return getValue().intValue(); } /** * Converts the value of this {@code Field} into a primitive {@code int} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code int} value. */ @Override public final int intValue(int orElse) { return getValue().intValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code long} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code long} value. */ @Override public final long longValue() { return getValue().longValue(); } /** * Converts the value of this {@code Field} into a primitive {@code long} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code long} value. */ @Override public final long longValue(long orElse) { return getValue().longValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code float} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code float} value. */ @Override public final float floatValue() { return getValue().floatValue(); } /** * Converts the value of this {@code Field} into a primitive {@code float} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code float} value. */ @Override public final float floatValue(float orElse) { return getValue().floatValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code double} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code double} value. */ @Override public final double doubleValue() { return getValue().doubleValue(); } /** * Converts the value of this {@code Field} into a primitive {@code double} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code double} value. */ @Override public final double doubleValue(double orElse) { return getValue().doubleValue(orElse); } /** * Converts the value of this {@code Field} into a {@code BigInteger} value, * if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a {@code BigInteger} value. */ @Override public final BigInteger integerValue() { return getValue().integerValue(); } /** * Converts the value of this {@code Field} into a {@code BigInteger} value, * if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a {@code BigInteger} value. */ @Override public final BigInteger integerValue(BigInteger orElse) { return getValue().integerValue(orElse); } /** * Converts the value of this {@code Field} into a {@code Number} object, * if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a {@code Number} object. */ @Override public final Number numberValue() { return getValue().numberValue(); } /** * Converts the value of this {@code Field} into a {@code Number} object, * if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a {@code Number} object. */ @Override public final Number numberValue(Number orElse) { return getValue().numberValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code char} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code char} value. */ @Override public final char charValue() { return getValue().charValue(); } /** * Converts the value of this {@code Field} into a primitive {@code char} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code char} value. */ @Override public final char charValue(char orElse) { return getValue().charValue(orElse); } /** * Converts the value of this {@code Field} into a primitive {@code boolean} * value, if possible. * * @throws UnsupportedOperationException if the value of this {@code Field} * can't be converted into a primitive {@code boolean} value. */ @Override public final boolean booleanValue() { return getValue().booleanValue(); } /** * Converts the value of this {@code Field} into a primitive {@code boolean} * value, if possible; otherwise returns {@code orElse} if the value of this * {@code Field} can't be converted into a primitive {@code boolean} value. */ @Override public final boolean booleanValue(boolean orElse) { return getValue().booleanValue(orElse); } @Override public abstract Field branch(); @Override public abstract Field commit(); static final int IMMUTABLE = 1 << 0; static final AtomicIntegerFieldUpdater<Field> FLAGS = AtomicIntegerFieldUpdater.newUpdater(Field.class, "flags"); public static Field of(Object object) { if (object instanceof Field) { return (Field) object; } else if (object instanceof Map.Entry<?, ?>) { final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object; return Slot.of(Value.fromObject(entry.getKey()), Value.fromObject(entry.getValue())); } else { throw new IllegalArgumentException(object.toString()); } } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Form.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collection; import java.util.Deque; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TreeMap; import java.util.TreeSet; import swim.structure.form.ArrayForm; import swim.structure.form.BigIntegerForm; import swim.structure.form.BooleanForm; import swim.structure.form.ByteBufferForm; import swim.structure.form.ByteForm; import swim.structure.form.CharacterForm; import swim.structure.form.CollectionForm; import swim.structure.form.DoubleForm; import swim.structure.form.FloatForm; import swim.structure.form.IntegerForm; import swim.structure.form.ItemForm; import swim.structure.form.LongForm; import swim.structure.form.MapForm; import swim.structure.form.NumberForm; import swim.structure.form.PolyForm; import swim.structure.form.ShortForm; import swim.structure.form.StringForm; import swim.structure.form.TagForm; import swim.structure.form.UnitForm; import swim.structure.form.ValueForm; /** * Transformation between a structurally typed {@link Item} and a nominally * typed Java object. */ public abstract class Form<T> { /** * Returns the key of the tag attribute that distinguishes structures of this * {@code Form}; returns {@code null} if this {@code Form} has no * distinguishing tag attribute. Used to accelerate distrcrimination of * polymorphic structural types with nominal type hints. */ public String tag() { return null; } /** * Returns a version of this {@code Form} that requires a head {@link Attr} * with the given {@code tag} name. */ public Form<T> tag(String tag) { if (tag != null) { return new TagForm<T>(tag, this); } else { return this; } } /** * Returns a default–possibly {@code null}–value of type {@code T}. Used * as the fallback return value when {@link Item#coerce(Form) coercing} an * invalid structural value. */ public T unit() { return null; } /** * Returns a version of this {@code Form} with the given {@code unit} value. */ public Form<T> unit(T unit) { if (unit != null) { return new UnitForm<T>(unit, this); } else { return this; } } /** * Returns the reified {@code Class} of type {@code T}. */ public abstract Class<?> type(); /** * Converts a nominally typed Java {@code object} into its structurally typed * equivalent based on the provided prototype {@code item}. The passed-in * {@code item} is assumed to be non-{@code null}. The returned {@code Item} * must never be {@code null}. */ public Item mold(T object, Item item) { return item.concat(mold(object)); } /** * Converts a nominally typed Java {@code object} into its structurally typed * equivalent. The returned {@code Item} must never be {@code null}. */ public abstract Item mold(T object); /** * Converts a structurally typed {@code item} into a nominally typed Java * object based on the provided prototype {@code object}. The passed-in * {@code item} is assumed to be non-{@code null}. The passed-in prototype * {@code object} may be {@code null}. */ public T cast(Item item, T object) { return cast(item); } /** * Converts a structurally typed {@code item} into a nominally typed Java * object. The passed-in {@code item} is assumed to be non-{@code null}. */ public abstract T cast(Item item); private static Form<Byte> byteForm; private static Form<Short> shortForm; private static Form<Integer> integerForm; private static Form<Long> longForm; private static Form<Float> floatForm; private static Form<Double> doubleForm; private static Form<Character> characterForm; private static Form<Boolean> booleanForm; private static Form<BigInteger> bigIntegerForm; private static Form<Number> numberForm; private static Form<String> stringForm; private static Form<ByteBuffer> byteBufferForm; private static Form<Item> itemForm; private static Form<Value> valueForm; /** * Utility method to receive a singleton {@link ByteForm}. */ public static Form<Byte> forByte() { if (byteForm == null) { byteForm = new ByteForm((byte) 0); } return byteForm; } /** * Utility method to receive a singleton {@link ShortForm}. */ public static Form<Short> forShort() { if (shortForm == null) { shortForm = new ShortForm((short) 0); } return shortForm; } /** * Utility method to receive a singleton {@link IntegerForm}. */ public static Form<Integer> forInteger() { if (integerForm == null) { integerForm = new IntegerForm(0); } return integerForm; } /** * Utility method to receive a singleton {@link LongForm}. */ public static Form<Long> forLong() { if (longForm == null) { longForm = new LongForm(0L); } return longForm; } /** * Utility method to receive a singleton {@link FloatForm}. */ public static Form<Float> forFloat() { if (floatForm == null) { floatForm = new FloatForm(0.0f); } return floatForm; } /** * Utility method to receive a singleton {@link DoubleForm}. */ public static Form<Double> forDouble() { if (doubleForm == null) { doubleForm = new DoubleForm(0.0); } return doubleForm; } /** * Utility method to receive a singleton {@link CharacterForm}. */ public static Form<Character> forCharacter() { if (characterForm == null) { characterForm = new CharacterForm('\0'); } return characterForm; } /** * Utility method to receive a singleton {@link BooleanForm}. */ public static Form<Boolean> forBoolean() { if (booleanForm == null) { booleanForm = new BooleanForm(false); } return booleanForm; } /** * Utility method to receive a singleton {@link BigIntegerForm}. */ public static final Form<BigInteger> forBigInteger() { if (bigIntegerForm == null) { bigIntegerForm = new BigIntegerForm(BigInteger.ZERO); } return bigIntegerForm; } /** * Utility method to receive a singleton {@link NumberForm}. */ public static Form<Number> forNumber() { if (numberForm == null) { numberForm = new NumberForm(Integer.valueOf(0)); } return numberForm; } /** * Utility method to receive a singleton {@link StringForm}. */ public static Form<String> forString() { if (stringForm == null) { stringForm = new StringForm(""); } return stringForm; } /** * Utility method to receive a singleton {@link ByteBufferForm}. */ public static Form<ByteBuffer> forByteBuffer() { if (byteBufferForm == null) { byteBufferForm = new ByteBufferForm(); } return byteBufferForm; } /** * Utility method to receive a singleton {@link ItemForm}. */ public static Form<Item> forItem() { if (itemForm == null) { itemForm = new ItemForm(Item.absent()); } return itemForm; } /** * Utility method to receive a singleton {@link ValueForm}. */ public static Form<Value> forValue() { if (valueForm == null) { valueForm = new ValueForm(Value.absent()); } return valueForm; } /** * Utility method to construct an {@link ArrayForm}. */ @SuppressWarnings("unchecked") public static <T> Form<T> forArray(Class<?> type, Form<?> form) { return (Form<T>) new ArrayForm(type, form); } /** * Utility method to construct a {@link CollectionForm}. */ @SuppressWarnings("unchecked") public static <CC, T> Form<CC> forCollection(Class<?> type, Form<T> form) { if (type == Collection.class || type == List.class) { type = ArrayList.class; } else if (type == Queue.class || type == Deque.class) { type = LinkedList.class; } else if (type == Set.class) { type = HashSet.class; } else if (type == SortedSet.class) { type = TreeSet.class; } return (Form<CC>) new CollectionForm<T>(type, form); } /** * Utility method to construct a {@link CollectionForm} where the underlying * collection is of type {@link java.util.List List&lt;T&gt;}. */ public static <T> Form<List<T>> forList(Form<T> form) { return forCollection(List.class, form); } /** * Utility method to construct a {@link CollectionForm} where the underlying * collection is of type {@link java.util.Set List&lt;T&gt;}. */ public static <T> Form<Set<T>> forSet(Form<T> form) { return forCollection(Set.class, form); } /** * Utility method to construct a {@link MapForm} where {@link Form#cast(Item) * casts} return objects of type {@code type}. * * @throws ClassCastException if {@code type} does not extend {@link * java.util.Map} */ @SuppressWarnings("unchecked") public static <CC, K, V> Form<CC> forMap(Class<?> type, Form<K> keyForm, Form<V> valForm) { if (type == Map.class) { type = HashMap.class; } else if (type == SortedMap.class) { type = TreeMap.class; } return (Form<CC>) (Form<?>) new MapForm<K, V>(type, keyForm, valForm); } /** * Utility method to construct a {@link MapForm}. */ public static <K, V> Form<Map<K, V>> forMap(Form<K> keyForm, Form<V> valForm) { return forMap(Map.class, keyForm, valForm); } /** * Returns whether {@code type} has a built-in base (i.e. is defined in {@code * swim.structure.form} and is not a {@code CollectionForm}) {@code Form}. */ public static boolean isBuiltin(Class<?> type) { return type.isPrimitive() || type.isArray() || type == Object.class || String.class.isAssignableFrom(type) || Number.class.isAssignableFrom(type) || Character.class.isAssignableFrom(type) || Boolean.class.isAssignableFrom(type) || ByteBuffer.class.isAssignableFrom(type); } /** * Returns the {@code type} built-in {@code Form} for {@code type} if it * exists, and {@code null} if it does not. */ @SuppressWarnings("unchecked") public static <T> Form<T> forBuiltin(Class<?> type) { if (type == String.class) { return (Form<T>) forString(); } else if (type == Byte.class || type == Byte.TYPE) { return (Form<T>) forByte(); } else if (type == Short.class || type == Short.TYPE) { return (Form<T>) forShort(); } else if (type == Integer.class || type == Integer.TYPE) { return (Form<T>) forInteger(); } else if (type == Long.class || type == Long.TYPE) { return (Form<T>) forLong(); } else if (type == Float.class || type == Float.TYPE) { return (Form<T>) forFloat(); } else if (type == Double.class || type == Double.TYPE) { return (Form<T>) forDouble(); } else if (type == Character.class || type == Character.TYPE) { return (Form<T>) forCharacter(); } else if (type == Boolean.class || type == Boolean.TYPE) { return (Form<T>) forBoolean(); } else if (type == BigInteger.class) { return (Form<T>) forBigInteger(); } else if (type == ByteBuffer.class) { return (Form<T>) forByteBuffer(); } else if (Value.class.isAssignableFrom(type)) { return (Form<T>) forValue(); } else if (Item.class.isAssignableFrom(type)) { return (Form<T>) forItem(); } else { return null; } } /** * Returns a {@code Form} for {@code type} against {@code scope} preferring * built-in {@code Forms} to {@link swim.structure.form.ClassForm} * constructions whenever possible. */ public static <T> Form<T> forClass(Class<?> type, PolyForm scope) { if (type.isArray()) { final Class<?> componentType = type.getComponentType(); return forArray(componentType, forClass(componentType)); } else { Form<T> form = forBuiltin(type); if (form != null) { return form; } if (scope == null) { scope = new PolyForm(); } form = scope.reflectClass(type); if (form != null) { return form; } return null; } } /** * Returns a {@code Form} for {@code type} preferring built-in {@code Forms} * to {@link swim.structure.form.ClassForm} constructions whenever possible. */ public static <T> Form<T> forClass(Class<?> type) { return forClass(type, null); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/FormException.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; public class FormException extends RuntimeException { private static final long serialVersionUID = 1L; public FormException(String message, Throwable cause) { super(message, cause); } public FormException(String message) { super(message); } public FormException(Throwable cause) { super(cause); } public FormException() { super(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Func.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.structure.operator.InvokeOperator; public abstract class Func extends Expression { public abstract Item invoke(Value args, Interpreter interpreter, InvokeOperator operator); public Item expand(Value args, Interpreter interpreter, InvokeOperator operator) { return null; } @Override public boolean isConstant() { return false; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Header.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Header { String value() default ""; }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Interpreter.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; public class Interpreter { protected InterpreterSettings settings; Item[] scopeStack; int scopeDepth; protected Interpreter(InterpreterSettings settings, Item[] scopeStack, int scopeDepth) { this.settings = settings; this.scopeStack = scopeStack; this.scopeDepth = scopeDepth; } public Interpreter(InterpreterSettings settings) { this(settings, null, 0); } public Interpreter() { this(InterpreterSettings.standard(), null, 0); } public final InterpreterSettings settings() { return this.settings; } public Interpreter settings(InterpreterSettings settings) { this.settings = settings; return this; } public final int scopeDepth() { return this.scopeDepth; } public Item peekScope() { final int scopeDepth = this.scopeDepth; if (scopeDepth <= 0) { throw new InterpreterException("scope stack empty"); } return this.scopeStack[scopeDepth - 1]; } public Item getScope(int index) { if (index < 0 || index >= this.scopeDepth) { throw new IndexOutOfBoundsException(Integer.toString(index)); } return this.scopeStack[index]; } public void pushScope(Item scope) { final int scopeDepth = this.scopeDepth; if (scopeDepth >= this.settings.maxScopeDepth) { throw new InterpreterException("scope stack overflow"); } final Item[] oldScopeStack = this.scopeStack; final Item[] newScopeStack; if (oldScopeStack == null || scopeDepth + 1 > oldScopeStack.length) { newScopeStack = new Item[expand(scopeDepth + 1)]; if (oldScopeStack != null) { System.arraycopy(oldScopeStack, 0, newScopeStack, 0, scopeDepth); } this.scopeStack = newScopeStack; } else { newScopeStack = oldScopeStack; } newScopeStack[scopeDepth] = scope; this.scopeDepth = scopeDepth + 1; } public Item popScope() { final int scopeDepth = this.scopeDepth; if (scopeDepth <= 0) { throw new InterpreterException("scope stack empty"); } final Item[] scopeStack = this.scopeStack; final Item scope = scopeStack[scopeDepth - 1]; scopeStack[scopeDepth - 1] = null; this.scopeDepth = scopeDepth - 1; return scope; } public Item swapScope(Item newScope) { final int scopeDepth = this.scopeDepth; if (scopeDepth <= 0) { throw new InterpreterException("scope stack empty"); } final Item[] scopeStack = this.scopeStack; final Item oldScope = scopeStack[scopeDepth - 1]; scopeStack[scopeDepth - 1] = newScope; return oldScope; } public void willOperate(Operator operator) { // stub } public void didOperate(Operator operator, Item result) { // stub } public void willSelect(Selector selector) { // stub } public void didSelect(Selector selector, Object result) { // stub } public void willTransform(Selector selector) { // stub } public void didTransform(Selector selector, Item result) { // stub } static int expand(int n) { n = Math.max(32, n) - 1; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n + 1; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/InterpreterException.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; public class InterpreterException extends RuntimeException { private static final long serialVersionUID = 1L; public InterpreterException(String message, Throwable cause) { super(message, cause); } public InterpreterException(String message) { super(message); } public InterpreterException(Throwable cause) { super(cause); } public InterpreterException() { super(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/InterpreterSettings.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Debug; import swim.codec.Format; import swim.codec.Output; import swim.util.Murmur3; public class InterpreterSettings implements Debug { protected final int maxScopeDepth; public InterpreterSettings(int maxScopeDepth) { this.maxScopeDepth = maxScopeDepth; } public final int maxScopeDepth() { return this.maxScopeDepth; } public InterpreterSettings maxScopeDepth(int maxScopeDepth) { return copy(maxScopeDepth); } protected InterpreterSettings copy(int maxScopeDepth) { return new InterpreterSettings(maxScopeDepth); } protected boolean canEqual(Object other) { return other instanceof InterpreterSettings; } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof InterpreterSettings) { final InterpreterSettings that = (InterpreterSettings) other; return that.canEqual(this) && this.maxScopeDepth == that.maxScopeDepth; } return false; } @Override public int hashCode() { if (hashSeed == 0) { hashSeed = Murmur3.seed(InterpreterSettings.class); } return Murmur3.mash(Murmur3.mix(hashSeed, this.maxScopeDepth)); } @Override public void debug(Output<?> output) { output = output.write("new").write(' ').write("InterpreterSettings").write('(') .debug(this.maxScopeDepth).write(')'); } @Override public String toString() { return Format.debug(this); } public static final int MAX_SCOPE_DEPTH; private static InterpreterSettings standard; private static int hashSeed; public static InterpreterSettings standard() { if (standard == null) { standard = new InterpreterSettings(MAX_SCOPE_DEPTH); } return standard; } static { int maxScopeDepth; try { maxScopeDepth = Integer.parseInt(System.getProperty("swim.structure.interpreter.max.scope.depth")); } catch (NumberFormatException e) { maxScopeDepth = 1024; } MAX_SCOPE_DEPTH = maxScopeDepth; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Item.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import java.util.Iterator; import java.util.Map; import swim.codec.Debug; import swim.codec.Display; import swim.codec.Format; import swim.codec.Output; import swim.structure.func.MathModule; public abstract class Item implements Comparable<Item>, Iterable<Item>, Debug, Display { Item() { // stub } /** * Returns {@code true} if this {@code Item} is not {@link Absent}. */ public abstract boolean isDefined(); /** * Returns {@code true} if this {@code Item} is neither {@link Extant} nor * {@link Absent}. */ public abstract boolean isDistinct(); /** * Returns {@code true} if this {@code Item} always {@link * #evaluate(Interpreter) evaluates} to the same {@code Item}. */ public abstract boolean isConstant(); /** * Returns the key component of this {@code Item}, if this {@code Item} is a * {@link Field}; otherwise returns {@link Absent} if this {@code Item} is a * {@code Value}. */ public abstract Value key(); /** * Returns the value component of this {@code Item}, if this {@code Item} is * a {@link Field}; otherwise returns {@code this} if this {@code Item} is * a {@code Value}. */ public abstract Value toValue(); /** * Returns the {@code key} string of the first member of this {@code Item}, * if this {@code Item} is a {@link Record}, and its first member is an {@link * Attr}; otherwise returns {@code null} if this {@code Item} is not a {@code * Record}, or if this {@code Item} is a {@code Record} whose first member is * not an {@code Attr}. * <p> * Used to concisely get the name of the discriminating attribute of a * structure. The {@code tag} can be used to discern the nominal type of a * polymorphic structure, similar to an XML element tag. */ public abstract String tag(); /** * Returns the {@link #flattened() flattened} members of this {@code Item} * after all attributes have been removed, if this {@code Item} is a {@link * Record}; otherwise returns {@code this} if this {@code Item} is a * non-{@code Record} {@code Value}, or returns the value component if this * {@code Item} is a {@code Field}. * <p> * Used to concisely get the scalar value of an attributed structure. * An attributed structure is a {@code Record} with one or more attributes * that modify one or more other members. */ public abstract Value target(); /** * Returns the sole member of this {@code Item}, if this {@code Item} is a * {@link Record} with exactly one member, and its member is a {@code Value}; * returns {@link Extant} if this {@code Item} is an empty {@code Record}; * returns {@link Absent} if this {@code Item} is a {@code Field}; otherwise * returns {@code this} if this {@code Item} is a {@code Record} with more * than one member, or if this {@code Item} is a non-{@code Record} {@code * Value}. * <p> * Used to convert a unary {@code Record} into its member {@code Value}. * Facilitates writing code that treats a unary {@code Record} equivalently * to a bare {@code Value}. */ public abstract Value flattened(); /** * Returns {@code this} if this {@code Item} is a {@link Record}; returns a * {@code Record} containing just this {@code Item}, if this {@code Item} is * {@link #isDistinct() distinct}; otherwise returns an empty {@code Record} * if this {@code Item} is {@link Extant} or {@link Absent}. Facilitates * writing code that treats a bare {@code Value} equivalently to a unary * {@code Record}. */ public abstract Record unflattened(); /** * Returns the value of the first member of this {@code Item}, if this {@code * Item} is a {@link Record}, and its first member is an {@link Attr} whose * {@code key} string is equal to {@code tag}; otherwise returns {@link * Absent} if this {@code Item} is not a {@code Record}, or if this {@code * Item} is a {@code Record} whose first member is not an {@code Attr}, or if * this {@code Item} is a {@code Record} whose first member is an {@code * Attr} whose {@code key} does not equal the {@code tag}. * <p> * Used to conditionally get the value of the head {@code Attr} of a * structure, if and only if the key string of the head {@code Attr} is equal * to the {@code tag}. Can be used to check if a structure might conform to * a nominal type named {@code tag}, while simultaneously getting the value * of the {@code tag} attribute. */ public abstract Value header(String tag); /** * Returns the {@link #unflattened() unflattened} {@link #header(String) * header} of this {@code Item}, if this {@code Item} is a {@link Record}, * and its first member is an {@link Attr} whose {@code key} string is equal * to {@code tag}; otherwise returns {@code null}. * <p> * The {@code headers} of the {@code tag} attribute of a structure are like * the attributes of an XML element tag; through unlike an XML element, * {@code tag} attribute headers are not limited to string keys and values. */ public abstract Record headers(String tag); /** * Returns the first member of this {@code Item}, if this {@code Item} is a * non-empty {@link Record}; otherwise returns {@link Absent}. */ public abstract Item head(); /** * Returns a view of all but the first member of this {@code Item}, if this * {@code Item} is a non-empty {@link Record}; otherwise returns an empty * {@code Record} if this {@code Item} is not a {@code Record}, or if this * {@code Item} is itself an empty {@code Record}. */ public abstract Record tail(); /** * Returns the {@link Record#flattened() flattened} {@link #tail() tail} * of this {@code Item}. Used to recursively deconstruct a structure, * terminating with its last {@code Value}, rather than a unary {@code * Record} containing its last value, if the structure ends with a {@code * Value} member. */ public abstract Value body(); /** * Returns the number of members contained in this {@code Item}, if this * {@code Item} is a {@link Record}; otherwise returns {@code 0} if this * {@code Item} is not a {@code Record}. */ public abstract int length(); /** * Returns {@code true} if this {@code Item} is a {@link Record} that has a * member equal to {@code item}; otherwise returns {@code false} if this * {@code Item} is not a {@code Record}, or if this {@code Item} is a {@code * Record}, but has no member equal to {@code item}. */ public abstract boolean contains(Item item); /** * Returns {@code true} if this {@code Item} is a {@link Record} that has a * {@link Field} member with a key that is equal to the given {@code key}; * otherwise returns {@code false} if this {@code Item} is not a {@code * Record}, or if this {@code Item} is a {@code Record}, but has no {@code * Field} member with a key equal to the given {@code key}. */ public abstract boolean containsKey(Value key); /** * Returns {@code true} if this {@code Item} is a {@link Record} that has a * {@link Field} with a {@code Text} key whose string value is equal to the * given {@code key}; otherwise returns {@code false} if this {@code Item} is * not a {@code Record}, or if this {@code Item} is a {@code Record}, but has * no {@code Field} member with a {@code Text} key whose string value equals * the given {@code key}. Equivalent to {@link #containsKey(Value)}, but * avoids boxing the {@code key} string into a {@code Text} value. */ public abstract boolean containsKey(String key); /** * Returns {@code true} if this {@code Item} is a {@link Record} that has a * {@link Field} member with a value that is equal to the given {@code value}; * otherwise returns {@code false} if this {@code Item} is not a {@code * Record}, or if this {@code Item} is a {@code Record}, but has no {@code * Field} member with a value equal to the given {@code value}. */ public abstract boolean containsValue(Value value); /** * Returns the value of the last {@link Field} member of this {@code Item} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Item} is not a {@link Record}, or if this {@code Item} is a * {@code Record}, but has no {@code Field} member with a key equal to the * given {@code key}. */ public abstract Value get(Value key); /** * Returns the value of the last {@link Field} member of this {@code Item} * with a {@code Text} key whose string value is equal to the given {@code * key}; returns {@link Absent} if this {@code Item} is not a {@link Record}, * or if this {@code Item} is a {@code Record}, but has no {@code Field} * member with a {@code Text} key whose string value equals the given {@code * key}. Equivalent to {@link #get(Value)}, but avoids boxing the {@code * key} string into a {@code Text} value. */ public abstract Value get(String key); /** * Returns the value of the last {@link Attr} member of this {@code Item} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Item} is not a {@link Record}, or if this {@code Item} is a * {@code Record}, but has no {@code Attr} member with a key equal to the * given {@code key}. */ public abstract Value getAttr(Text key); /** * Returns the value of the last {@link Attr} member of this {@code Item} * with a {@code Text} key whose string value is equal to the given {@code * key}; returns {@link Absent} if this {@code Item} is not a {@link Record}, * or if this {@code Item} is a {@code Record}, but has no {@code Attr} * member with a {@code Text} key whose string value equals the given {@code * key}. Equivalent to {@link #getAttr(Text)}, but avoids boxing the {@code * key} string into a {@code Text} value. */ public abstract Value getAttr(String key); /** * Returns the value of the last {@link Slot} member of this {@code Item} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Item} is not a {@link Record}, or if this {@code Item} is a * {@code Record}, but has no {@code Slot} member with a key equal to the * given {@code key}. */ public abstract Value getSlot(Value key); /** * Returns the value of the last {@link Slot} member of this {@code Item} * with a {@code Text} key whose string value is equal to the given {@code * key}; returns {@link Absent} if this {@code Item} is not a {@link Record}, * or if this {@code Item} is a {@code Record}, but has no {@code Slot} * member with a {@code Text} key whose string value equals the given {@code * key}. Equivalent to {@link #getSlot(Value)}, but avoids boxing the {@code * key} string into a {@code Text} value. */ public abstract Value getSlot(String key); /** * Returns the last {@link Field} member of this {@code Item} whose key is * equal to the given {@code key}; returns {@code null} if this {@code Item} * is not a {@link Record}, or if this {@code Item} is a {@code Record}, but * has no {@code Field} member with a {@code key} equal to the given * {@code key}. */ public abstract Field getField(Value key); /** * Returns the last {@link Field} member of this {@code Item} with a {@code * Text} key whose string value is equal to the given {@code key}; returns * {@code null} if this {@code Item} is not a {@link Record}, or if this * {@code Item} is a {@code Record}, but has no {@code Field} member with a * {@code Text} key whose string value equals the given {@code key}. * Equivalent to {@link #getField(Value)}, but avoids boxing the {@code key} * string into a {@code Text} value. */ public abstract Field getField(String key); /** * Returns the member of this {@code Item} at the given {@code index}, if * this {@code Item} is a {@link Record}, and the {@code index} is greater * than or equal to zero, and less than the {@link Record#length() length} of * the {@code Record}; otherwise returns {@link Absent} if this {@code Item} * is not a {@code Record}, or if this {@code Item} is a {@code Record}, but * the {@code index} is out of bounds. */ public abstract Item getItem(int index); public Record updated(Value key, Value value) { final Record record = Record.create(2); record.add(this); record.put(key, value); return record; } public Record updated(Value key, String value) { return updated(key, Text.from(value)); } public Record updated(Value key, int value) { return updated(key, Num.from(value)); } public Record updated(Value key, long value) { return updated(key, Num.from(value)); } public Record updated(Value key, float value) { return updated(key, Num.from(value)); } public Record updated(Value key, double value) { return updated(key, Num.from(value)); } public Record updated(Value key, boolean value) { return updated(key, Bool.from(value)); } public Record updated(String key, Value value) { return updated(Text.from(key), value); } public Record updated(String key, String value) { return updated(key, Text.from(value)); } public Record updated(String key, int value) { return updated(key, Num.from(value)); } public Record updated(String key, long value) { return updated(key, Num.from(value)); } public Record updated(String key, float value) { return updated(key, Num.from(value)); } public Record updated(String key, double value) { return updated(key, Num.from(value)); } public Record updated(String key, boolean value) { return updated(key, Bool.from(value)); } public Record updatedAttr(Text key, Value value) { final Record record = Record.create(2); record.add(this); record.putAttr(key, value); return record; } public Record updatedAttr(Text key, String value) { return updatedAttr(key, Text.from(value)); } public Record updatedAttr(Text key, int value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(Text key, long value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(Text key, float value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(Text key, double value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(Text key, boolean value) { return updatedAttr(key, Bool.from(value)); } public Record updatedAttr(String key, Value value) { return updatedAttr(Text.from(key), value); } public Record updatedAttr(String key, String value) { return updatedAttr(key, Text.from(value)); } public Record updatedAttr(String key, int value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(String key, long value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(String key, float value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(String key, double value) { return updatedAttr(key, Num.from(value)); } public Record updatedAttr(String key, boolean value) { return updatedAttr(key, Bool.from(value)); } public Record updatedSlot(Value key, Value value) { final Record record = Record.create(2); record.add(this); record.putSlot(key, value); return record; } public Record updatedSlot(Value key, String value) { return updatedSlot(key, Text.from(value)); } public Record updatedSlot(Value key, int value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(Value key, long value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(Value key, float value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(Value key, double value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(Value key, boolean value) { return updatedSlot(key, Bool.from(value)); } public Record updatedSlot(String key, Value value) { return updatedSlot(Text.from(key), value); } public Record updatedSlot(String key, String value) { return updatedSlot(key, Text.from(value)); } public Record updatedSlot(String key, int value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(String key, long value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(String key, float value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(String key, double value) { return updatedSlot(key, Num.from(value)); } public Record updatedSlot(String key, boolean value) { return updatedSlot(key, Bool.from(value)); } public Record appended(Item item) { final Record record = Record.create(2); record.add(this); record.add(item); return record; } public Record appended(String item) { return appended(Text.from(item)); } public Record appended(int item) { return appended(Num.from(item)); } public Record appended(long item) { return appended(Num.from(item)); } public Record appended(float item) { return appended(Num.from(item)); } public Record appended(double item) { return appended(Num.from(item)); } public Record appended(boolean item) { return appended(Bool.from(item)); } public Record appended(Object... items) { final Record record = Record.create(1 + items.length); record.add(this); record.addAll(Record.of(items)); return record; } public Record prepended(Item item) { final Record record = Record.create(2); record.add(item); record.add(this); return record; } public Record prepended(String item) { return prepended(Text.from(item)); } public Record prepended(int item) { return prepended(Num.from(item)); } public Record prepended(long item) { return prepended(Num.from(item)); } public Record prepended(float item) { return prepended(Num.from(item)); } public Record prepended(double item) { return prepended(Num.from(item)); } public Record prepended(boolean item) { return prepended(Bool.from(item)); } public Record prepended(Object... items) { final Record record = Record.create(items.length + 1); record.addAll(Record.of(items)); record.add(this); return record; } public abstract Item removed(Value key); public abstract Item removed(String key); public Record concat(Item that) { final Record record = Record.create(1 + that.length()); record.add(this); if (that instanceof Record) { record.addAll((Record) that); } else { record.add(that); } return record; } public abstract Item conditional(Item thenTerm, Item elseTerm); public abstract Item or(Item that); public abstract Item and(Item that); public abstract Item bitwiseOr(Item that); public abstract Item bitwiseXor(Item that); public abstract Item bitwiseAnd(Item that); public Item lt(Item that) { return compareTo(that) < 0 ? Bool.from(true) : Item.absent(); } public Item le(Item that) { return compareTo(that) <= 0 ? Bool.from(true) : Item.absent(); } public Item eq(Item that) { return equals(that) ? Bool.from(true) : Item.absent(); } public Item ne(Item that) { return !equals(that) ? Bool.from(true) : Item.absent(); } public Item ge(Item that) { return compareTo(that) >= 0 ? Bool.from(true) : Item.absent(); } public Item gt(Item that) { return compareTo(that) > 0 ? Bool.from(true) : Item.absent(); } public abstract Item plus(Item that); public abstract Item minus(Item that); public abstract Item times(Item that); public abstract Item divide(Item that); public abstract Item modulo(Item that); public abstract Item not(); public abstract Item bitwiseNot(); public abstract Item negative(); public abstract Item positive(); public abstract Item inverse(); public Item invoke(Value args) { return Item.absent(); } public abstract Value lambda(Value template); public Selector filter() { final Selector selector = Selector.literal(this); return selector.filter(); } public Selector filter(Item predicate) { final Selector selector = Selector.literal(this); return selector.filter(predicate); } public Item max(Item that) { return compareTo(that) >= 0 ? this : that; } public Item min(Item that) { return compareTo(that) <= 0 ? this : that; } public Item evaluate(Interpreter interpreter) { return this; } public Item evaluate(Item scope) { final Interpreter interpreter = new Interpreter(); interpreter.pushScope(Item.globalScope()); interpreter.pushScope(scope); return evaluate(interpreter); } public Item substitute(Interpreter interpreter) { return this; } public Item substitute(Item scope) { final Interpreter interpreter = new Interpreter(); interpreter.pushScope(Item.globalScope()); interpreter.pushScope(scope); return substitute(interpreter); } /** * Converts this {@code Item} into a {@code String} value, if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a {@code String} value. */ public abstract String stringValue(); /** * Converts this {@code Item} into a {@code String} value, if possible; * otherwise returns {@code orElse} if this {@code Item} can't be converted * into a {@code string} value. */ public abstract String stringValue(String orElse); /** * Converts this {@code Item} into a primitive {@code byte} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code byte} value. */ public abstract byte byteValue(); /** * Converts this {@code Item} into a primitive {@code byte} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code byte} value. */ public abstract byte byteValue(byte orElse); /** * Converts this {@code Item} into a primitive {@code short} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code short} value. */ public abstract short shortValue(); /** * Converts this {@code Item} into a primitive {@code short} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code short} value. */ public abstract short shortValue(short orElse); /** * Converts this {@code Item} into a primitive {@code int} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code int} value. */ public abstract int intValue(); /** * Converts this {@code Item} into a primitive {@code int} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code int} value. */ public abstract int intValue(int orElse); /** * Converts this {@code Item} into a primitive {@code long} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code long} value. */ public abstract long longValue(); /** * Converts this {@code Item} into a primitive {@code long} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code long} value. */ public abstract long longValue(long orElse); /** * Converts this {@code Item} into a primitive {@code float} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code float} value. */ public abstract float floatValue(); /** * Converts this {@code Item} into a primitive {@code float} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code float} value. */ public abstract float floatValue(float orElse); /** * Converts this {@code Item} into a primitive {@code double} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code double} value. */ public abstract double doubleValue(); /** * Converts this {@code Item} into a primitive {@code double} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code double} value. */ public abstract double doubleValue(double orElse); /** * Converts this {@code Item} into a {@code BigInteger} value, if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a {@code BigInteger} value. */ public abstract BigInteger integerValue(); /** * Converts this {@code Item} into a {@code BigInteger} value, if possible; * otherwise returns {@code orElse} if this {@code Item} can't * be converted into a {@code BigInteger} value. */ public abstract BigInteger integerValue(BigInteger orElse); /** * Converts this {@code Item} into a {@code Number} object, if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a {@code Number} object. */ public abstract Number numberValue(); /** * Converts this {@code Item} into a {@code Number} object, if possible; * otherwise returns {@code orElse} if this {@code Item} can't * be converted into a {@code Number} object. */ public abstract Number numberValue(Number orElse); /** * Converts this {@code Item} into a primitive {@code char} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code char} value. */ public abstract char charValue(); /** * Converts this {@code Item} into a primitive {@code char} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code char} value. */ public abstract char charValue(char orElse); /** * Converts this {@code Item} into a primitive {@code boolean} value, * if possible. * * @throws UnsupportedOperationException if this {@code Item} can't be * converted into a primitive {@code boolean} value. */ public abstract boolean booleanValue(); /** * Converts this {@code Item} into a primitive {@code boolean} value, * if possible; otherwise returns {@code orElse} if this {@code Item} can't * be converted into a primitive {@code boolean} value. */ public abstract boolean booleanValue(boolean orElse); public <T> T cast(Form<T> form) { return form.cast(this); } public <T> T cast(Form<T> form, T orElse) { T object = form.cast(this); if (object == null) { object = orElse; } return object; } public <T> T coerce(Form<T> form) { T object = form.cast(this); if (object == null) { object = form.unit(); } return object; } public <T> T coerce(Form<T> form, T orElse) { T object = form.cast(this); if (object == null) { object = form.unit(); } if (object == null) { object = orElse; } return object; } public abstract boolean isAliased(); public abstract boolean isMutable(); public abstract void alias(); public abstract Item branch(); /** * Flags this {@code Item} as immutable, recursively if it is a {@link * Record}, then returns this {@code Item}. */ public abstract Item commit(); public int precedence() { return 12; } @Override public Iterator<Item> iterator() { return new ItemIterator(this); } /** * Returns the heterogeneous sort order of this {@code Item}. Used to impose * a total order on the set of all items. When comparing two items of * different types, the items order according to their {@code typeOrder}. */ public abstract int typeOrder(); @Override public abstract int compareTo(Item other); public abstract boolean keyEquals(Object key); @Override public abstract boolean equals(Object other); @Override public abstract int hashCode(); public abstract void debug(Output<?> output); public void display(Output<?> output) { debug(output); } public String toString() { return Format.debug(this); } public static Item empty() { return Record.empty(); } public static Item extant() { return Extant.extant(); } public static Item absent() { return Absent.absent(); } public static Item fromObject(Object object) { if (object instanceof Item) { return (Item) object; } else if (object instanceof Map.Entry<?, ?>) { final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object; return Slot.of(Value.fromObject(entry.getKey()), Value.fromObject(entry.getValue())); } else { return Value.fromObject(object); } } private static Item globalScope; public static Item globalScope() { if (globalScope == null) { globalScope = Record.create(1) .slot("math", MathModule.scope()) .commit(); } return globalScope; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/ItemIterator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.Iterator; import java.util.NoSuchElementException; final class ItemIterator implements Iterator<Item> { Item item; ItemIterator(Item item) { this.item = item; } @Override public boolean hasNext() { return this.item != null; } @Override public Item next() { final Item item = this.item; if (item == null) { throw new NoSuchElementException(); } this.item = null; return item; } @Override public void remove() { throw new UnsupportedOperationException(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Kind.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Kind { }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Member.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface Member { String value() default ""; }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Num.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import swim.codec.Output; import swim.util.Murmur3; public abstract class Num extends Value { Num() { //stub } @Override public boolean isConstant() { return true; } public boolean isUint32() { return false; } public boolean isUint64() { return false; } public abstract boolean isNaN(); public abstract boolean isInfinite(); public abstract boolean isValidByte(); public abstract boolean isValidShort(); public abstract boolean isValidInt(); public abstract boolean isValidLong(); public abstract boolean isValidFloat(); public abstract boolean isValidDouble(); public abstract boolean isValidInteger(); /** * Converts this {@code Num} into a {@code String} value. */ @Override public abstract String stringValue(); /** * Converts this {@code Num} into a {@code String} value; equivalent to * {@link #stringValue()}. */ @Override public String stringValue(String orElse) { return stringValue(); } /** * Converts this {@code Num} into a primitive {@code byte} value. */ @Override public abstract byte byteValue(); /** * Converts this {@code Num} into a primitive {@code byte} value; equivalent * to {@link #byteValue()}. */ @Override public byte byteValue(byte orElse) { return byteValue(); } /** * Converts this {@code Num} into a primitive {@code short} value. */ @Override public abstract short shortValue(); /** * Converts this {@code Num} into a primitive {@code short} value; equivalent * {@link #shortValue()}. */ @Override public short shortValue(short orElse) { return shortValue(); } /** * Converts this {@code Num} into a primitive {@code int} value. */ @Override public abstract int intValue(); /** * Converts this {@code Num} into a primitive {@code int} value; equivalent * to {@link #intValue()}. */ @Override public int intValue(int orElse) { return intValue(); } /** * Converts this {@code Num} into a primitive {@code long} value. */ @Override public abstract long longValue(); /** * Converts this {@code Num} into a primitive {@code long} value; equivalent * to {@link #longValue()}. */ @Override public long longValue(long orElse) { return longValue(); } /** * Converts this {@code Num} into a primitive {@code float} value. */ @Override public abstract float floatValue(); /** * Converts this {@code Num} into a primitive {@code float} value; equivalent * to {@link #floatValue()}. */ @Override public float floatValue(float orElse) { return floatValue(); } /** * Converts this {@code Num} into a primitive {@code double} value. */ @Override public abstract double doubleValue(); /** * Converts this {@code Num} into a primitive {@code double} value; equivalent * to {@link #doubleValue()}. */ @Override public double doubleValue(double orElse) { return doubleValue(); } /** * Converts this {@code Num} into a {@code BigInteger} value. */ @Override public abstract BigInteger integerValue(); /** * Converts this {@code Num} into a {@code BigInteger} value; equivalent * to {@link #integerValue()}. */ @Override public BigInteger integerValue(BigInteger orElse) { return integerValue(); } /** * Converts this {@code Num} into a {@code Number} object. */ @Override public abstract Number numberValue(); /** * Converts this {@code Num} into a {@code Number} object; equivalent to * {@link #numberValue()}. */ @Override public Number numberValue(Number orElse) { return numberValue(); } /** * Converts this {@code Value} into a primitive {@code char} value. */ @Override public abstract char charValue(); /** * Converts this {@code Value} into a primitive {@code char} value; equivalent * to {@link #charValue()}. */ @Override public char charValue(char orElse) { return charValue(); } /** * Converts this {@code Value} into a primitive {@code boolean} value. */ @Override public abstract boolean booleanValue(); /** * Converts this {@code Value} into a primitive {@code boolean} value; * equivalent to {@link #booleanValue()}. */ @Override public boolean booleanValue(boolean orElse) { return booleanValue(); } @Override public Value bitwiseOr(Value that) { if (that instanceof Num) { return bitwiseOr((Num) that); } return super.bitwiseOr(that); } public abstract Value bitwiseOr(Num that); @Override public Value bitwiseXor(Value that) { if (that instanceof Num) { return bitwiseXor((Num) that); } return super.bitwiseXor(that); } public abstract Value bitwiseXor(Num that); @Override public Value bitwiseAnd(Value that) { if (that instanceof Num) { return bitwiseAnd((Num) that); } return super.bitwiseAnd(that); } public abstract Value bitwiseAnd(Num that); @Override public Value plus(Value that) { if (that instanceof Num) { return plus((Num) that); } return super.plus(that); } public abstract Num plus(Num that); @Override public Value minus(Value that) { if (that instanceof Num) { return minus((Num) that); } return super.minus(that); } public abstract Num minus(Num that); @Override public Value times(Value that) { if (that instanceof Num) { return times((Num) that); } return super.times(that); } public abstract Num times(Num that); @Override public Value divide(Value that) { if (that instanceof Num) { return divide((Num) that); } return super.divide(that); } public abstract Num divide(Num that); @Override public Value modulo(Value that) { if (that instanceof Num) { return modulo((Num) that); } return super.modulo(that); } public abstract Num modulo(Num that); @Override public abstract Value bitwiseNot(); @Override public abstract Num negative(); @Override public Num positive() { return this; } @Override public abstract Num inverse(); public abstract Num abs(); public abstract Num ceil(); public abstract Num floor(); public abstract Num round(); public abstract Num sqrt(); public abstract Num pow(Num that); public Num max(Num that) { return compareTo(that) >= 0 ? this : that; } public Num min(Num that) { return compareTo(that) <= 0 ? this : that; } @Override public int typeOrder() { return 6; } @Override public int compareTo(Item other) { if (other instanceof Num) { return compareTo((Num) other); } return Integer.compare(typeOrder(), other.typeOrder()); } public int compareTo(Num that) { if (isValidByte() && that.isValidByte()) { return Byte.compare(byteValue(), that.byteValue()); } else if (isValidShort() && that.isValidShort()) { return Short.compare(shortValue(), that.shortValue()); } else if (isValidInt() && that.isValidInt()) { return Integer.compare(intValue(), that.intValue()); } else if (isValidLong() && that.isValidLong()) { return Long.compare(longValue(), that.longValue()); } else if (isValidFloat() && that.isValidFloat()) { final float x = floatValue(); final float y = that.floatValue(); return x < y ? -1 : x > y ? 1 : Float.isNaN(y) ? (Float.isNaN(x) ? 0 : -1) : Float.isNaN(x) ? 1 : 0; } else if (isValidDouble() && that.isValidDouble()) { final double x = doubleValue(); final double y = that.doubleValue(); return x < y ? -1 : x > y ? 1 : Double.isNaN(y) ? (Double.isNaN(x) ? 0 : -1) : Double.isNaN(x) ? 1 : 0; } else { return stringValue().compareTo(that.stringValue()); } } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof Num) { return equals((Num) other); } else { return false; } } boolean equals(Num that) { if (isValidByte() && that.isValidByte()) { return byteValue() == that.byteValue(); } else if (isValidShort() && that.isValidShort()) { return shortValue() == that.shortValue(); } else if (isValidInt() && that.isValidInt()) { return intValue() == that.intValue(); } else if (isValidLong() && that.isValidLong()) { return longValue() == that.longValue(); } else if (isValidFloat() && that.isValidFloat()) { final float x = floatValue(); final float y = that.floatValue(); return x == y || Float.isNaN(x) && Float.isNaN(y); } else if (isValidDouble() && that.isValidDouble()) { final double x = doubleValue(); final double y = that.doubleValue(); return x == y || Double.isNaN(x) && Double.isNaN(y); } else { return stringValue().equals(that.stringValue()); } } @Override public int hashCode() { if (isValidByte()) { return Murmur3.hash(byteValue()); } else if (isValidShort()) { return Murmur3.hash(shortValue()); } else if (isValidInt()) { return Murmur3.hash(intValue()); } else if (isValidLong()) { return Murmur3.hash(longValue()); } else if (isValidFloat()) { return Murmur3.hash(floatValue()); } else if (isValidDouble()) { return Murmur3.hash(doubleValue()); } else { return stringValue().hashCode(); } } @Override public void debug(Output<?> output) { output = output.write("Num").write('.').write("from").write('(').display(this).write(')'); } @Override public abstract void display(Output<?> output); public static Num from(int value) { return NumI32.from(value); } public static Num from(long value) { return NumI64.from(value); } public static Num from(float value) { return NumF32.from(value); } public static Num from(double value) { return NumF64.from(value); } public static Num from(BigInteger value) { return NumInt.from(value); } public static Num from(Number value) { if (value instanceof Byte) { return from(value.byteValue()); } else if (value instanceof Short) { return from(value.shortValue()); } else if (value instanceof Integer) { return from(value.intValue()); } else if (value instanceof Long) { return from(value.longValue()); } else if (value instanceof Float) { return from(value.floatValue()); } else if (value instanceof Double) { return from(value.doubleValue()); } else if (value instanceof BigInteger) { return from((BigInteger) value); } else { return from(value.doubleValue()); } } public static Num from(char value) { return NumI32.from((int) value); } public static Num from(String value) { if ("NaN".equals(value)) { return NumF64.nan(); } else { try { final long longValue = Long.parseLong(value); if ((int) longValue == longValue) { return from((int) longValue); } else { return from(longValue); } } catch (NumberFormatException e1) { try { final double doubleValue = Double.parseDouble(value); if ((float) doubleValue == doubleValue) { return from((float) doubleValue); } else { return from(doubleValue); } } catch (NumberFormatException e2) { return from(new BigInteger(value)); } } } } public static Num uint32(int value) { return NumI32.uint32(value); } public static Num uint64(long value) { return NumI64.uint64(value); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/NumF32.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigDecimal; import java.math.BigInteger; import swim.codec.Format; import swim.codec.Output; import swim.util.HashGenCacheSet; final class NumF32 extends Num { final float value; NumF32(float value) { this.value = value; } @Override public boolean isNaN() { return Float.isNaN(this.value); } @Override public boolean isInfinite() { return Float.isInfinite(this.value); } @Override public boolean isValidByte() { return (byte) this.value == this.value; } @Override public boolean isValidShort() { return (short) this.value == this.value; } @Override public boolean isValidInt() { return (int) this.value == this.value; } @Override public boolean isValidLong() { return (long) this.value == this.value; } @Override public boolean isValidFloat() { return true; } @Override public boolean isValidDouble() { return true; } @Override public boolean isValidInteger() { return integerValue().floatValue() == this.value; } @Override public String stringValue() { return Float.toString(this.value); } @Override public byte byteValue() { return (byte) this.value; } @Override public short shortValue() { return (short) this.value; } @Override public int intValue() { return (int) this.value; } @Override public long longValue() { return (long) this.value; } @Override public float floatValue() { return this.value; } @Override public double doubleValue() { return this.value; } @Override public BigInteger integerValue() { return BigDecimal.valueOf(this.value).toBigInteger(); } @Override public Number numberValue() { return Float.valueOf(this.value); } @Override public char charValue() { return (char) this.value; } @Override public boolean booleanValue() { return this.value != 0.0f; } @Override public Value bitwiseOr(Num that) { return Value.absent(); } @Override public Value bitwiseXor(Num that) { return Value.absent(); } @Override public Value bitwiseAnd(Num that) { return Value.absent(); } @Override public Num plus(Num that) { if (that instanceof NumI32) { return NumF32.from(this.value + (float) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF32.from(this.value + (float) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from(this.value + ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value + ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value + ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num minus(Num that) { if (that instanceof NumI32) { return NumF32.from(this.value - (float) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF32.from(this.value - (float) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from(this.value - ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value - ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value - ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num times(Num that) { if (that instanceof NumI32) { return NumF32.from(this.value * (float) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF32.from(this.value * (float) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from(this.value * ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value * ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value * ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num divide(Num that) { if (that instanceof NumI32) { return NumF64.from((double) this.value / (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from((double) this.value / (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from((double) this.value / (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value / ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value / ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num modulo(Num that) { if (that instanceof NumI32) { return NumF32.from(this.value % (float) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF32.from(this.value % (float) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from(this.value % ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value % ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value % ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Value bitwiseNot() { return Value.absent(); } @Override public Num negative() { return NumF32.from(-this.value); } @Override public Num inverse() { return NumF64.from(1.0 / (double) this.value); } @Override public Num abs() { return NumF32.from(Math.abs(this.value)); } @Override public Num ceil() { return NumF32.from(Math.ceil(this.value)); } @Override public Num floor() { return NumF32.from(Math.floor(this.value)); } @Override public Num round() { return NumF32.from(Math.round(this.value)); } @Override public Num sqrt() { return NumF64.from(Math.sqrt((double) this.value)); } @Override public Num pow(Num that) { return NumF64.from(Math.pow((double) this.value, that.doubleValue())); } @Override public void display(Output<?> output) { Format.debugFloat(this.value, output); } private static NumF32 positiveZero; private static NumF32 negativeZero; private static NumF32 positiveOne; private static NumF32 negativeOne; private static NumF32 nan; private static HashGenCacheSet<NumF32> cache; static NumF32 positiveZero() { if (positiveZero == null) { positiveZero = new NumF32(0.0f); } return positiveZero; } static NumF32 negativeZero() { if (negativeZero == null) { negativeZero = new NumF32(-0.0f); } return negativeZero; } static NumF32 positiveOne() { if (positiveOne == null) { positiveOne = new NumF32(1.0f); } return positiveOne; } static NumF32 negativeOne() { if (negativeOne == null) { negativeOne = new NumF32(-1.0f); } return negativeOne; } static NumF32 nan() { if (nan == null) { nan = new NumF32(Float.NaN); } return nan; } public static NumF32 from(float value) { if (value == 0.0f) { if (Math.copySign(1.0f, value) == 1.0f) { return positiveZero(); } else { return negativeZero(); } } else if (value == 1.0f) { return positiveOne(); } else if (value == -1.0f) { return negativeOne(); } else if (Float.isNaN(value)) { return nan(); } else { return cache().put(new NumF32(value)); } } static HashGenCacheSet<NumF32> cache() { if (cache == null) { int cacheSize; try { cacheSize = Integer.parseInt(System.getProperty("swim.structure.num.f32.cache.size")); } catch (NumberFormatException e) { cacheSize = 16; } cache = new HashGenCacheSet<NumF32>(cacheSize); } return cache; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/NumF64.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigDecimal; import java.math.BigInteger; import swim.codec.Format; import swim.codec.Output; import swim.util.HashGenCacheSet; final class NumF64 extends Num { final double value; NumF64(double value) { this.value = value; } @Override public boolean isNaN() { return Double.isNaN(this.value); } @Override public boolean isInfinite() { return Double.isInfinite(this.value); } @Override public boolean isValidByte() { return (byte) this.value == this.value; } @Override public boolean isValidShort() { return (short) this.value == this.value; } @Override public boolean isValidInt() { return (int) this.value == this.value; } @Override public boolean isValidLong() { return (long) this.value == this.value; } @Override public boolean isValidFloat() { return (float) this.value == this.value; } @Override public boolean isValidDouble() { return true; } @Override public boolean isValidInteger() { return integerValue().doubleValue() == this.value; } @Override public String stringValue() { return Double.toString(this.value); } @Override public byte byteValue() { return (byte) this.value; } @Override public short shortValue() { return (short) this.value; } @Override public int intValue() { return (int) this.value; } @Override public long longValue() { return (long) this.value; } @Override public float floatValue() { return (float) this.value; } @Override public double doubleValue() { return this.value; } @Override public BigInteger integerValue() { return BigDecimal.valueOf(this.value).toBigInteger(); } @Override public Number numberValue() { return Double.valueOf(this.value); } @Override public char charValue() { return (char) this.value; } @Override public boolean booleanValue() { return this.value != 0.0; } @Override public Value bitwiseOr(Num that) { return Value.absent(); } @Override public Value bitwiseXor(Num that) { return Value.absent(); } @Override public Value bitwiseAnd(Num that) { return Value.absent(); } @Override public Num plus(Num that) { if (that instanceof NumI32) { return NumF64.from(this.value + (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from(this.value + (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from(this.value + (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value + ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from(this.value + ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num minus(Num that) { if (that instanceof NumI32) { return NumF64.from(this.value - (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from(this.value - (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from(this.value - (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value - ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from(this.value - ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num times(Num that) { if (that instanceof NumI32) { return NumF64.from(this.value * (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from(this.value * (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from(this.value * (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value * ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from(this.value * ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num divide(Num that) { if (that instanceof NumI32) { return NumF64.from(this.value / (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from(this.value / (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from(this.value / (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value / ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from(this.value / ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num modulo(Num that) { if (that instanceof NumI32) { return NumF64.from(this.value % (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from(this.value % (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from(this.value % (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value % ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from(this.value % ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Value bitwiseNot() { return Value.absent(); } @Override public Num negative() { return NumF64.from(-this.value); } @Override public Num inverse() { return NumF64.from(1.0 / this.value); } @Override public Num abs() { return NumF64.from(Math.abs(this.value)); } @Override public Num ceil() { return NumF64.from(Math.ceil(this.value)); } @Override public Num floor() { return NumF64.from(Math.floor(this.value)); } @Override public Num round() { return NumF64.from(Math.round(this.value)); } @Override public Num sqrt() { return NumF64.from(Math.sqrt(this.value)); } @Override public Num pow(Num that) { return NumF64.from(Math.pow(this.value, that.doubleValue())); } @Override public void display(Output<?> output) { Format.debugDouble(this.value, output); } private static NumF64 positiveZero; private static NumF64 negativeZero; private static NumF64 positiveOne; private static NumF64 negativeOne; private static NumF64 nan; private static HashGenCacheSet<NumF64> cache; static NumF64 positiveZero() { if (positiveZero == null) { positiveZero = new NumF64(0.0); } return positiveZero; } static NumF64 negativeZero() { if (negativeZero == null) { negativeZero = new NumF64(-0.0); } return negativeZero; } static NumF64 positiveOne() { if (positiveOne == null) { positiveOne = new NumF64(1.0); } return positiveOne; } static NumF64 negativeOne() { if (negativeOne == null) { negativeOne = new NumF64(-1.0); } return negativeOne; } static NumF64 nan() { if (nan == null) { nan = new NumF64(Double.NaN); } return nan; } public static NumF64 from(double value) { if (value == 0.0) { if (Math.copySign(1.0f, value) == 1.0f) { return positiveZero(); } else { return negativeZero(); } } else if (value == 1.0) { return positiveOne(); } else if (value == -1.0) { return negativeOne(); } else if (Double.isNaN(value)) { return nan(); } else { return cache().put(new NumF64(value)); } } static HashGenCacheSet<NumF64> cache() { if (cache == null) { int cacheSize; try { cacheSize = Integer.parseInt(System.getProperty("swim.structure.num.f64.cache.size")); } catch (NumberFormatException e) { cacheSize = 16; } cache = new HashGenCacheSet<NumF64>(cacheSize); } return cache; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/NumI32.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import swim.codec.Format; import swim.codec.Output; import swim.util.HashGenCacheSet; final class NumI32 extends Num { final int value; int flags; NumI32(int value, int flags) { this.value = value; this.flags = flags; } NumI32(int value) { this(value, 0); } @Override public boolean isUint32() { return (this.flags & UINT32) != 0; } @Override public boolean isNaN() { return false; } @Override public boolean isInfinite() { return false; } @Override public boolean isValidByte() { return (byte) this.value == this.value; } @Override public boolean isValidShort() { return (short) this.value == this.value; } @Override public boolean isValidInt() { return true; } @Override public boolean isValidLong() { return true; } @Override public boolean isValidFloat() { return true; } @Override public boolean isValidDouble() { return true; } @Override public boolean isValidInteger() { return true; } @Override public String stringValue() { return Integer.toString(this.value); } @Override public byte byteValue() { return (byte) this.value; } @Override public short shortValue() { return (short) this.value; } @Override public int intValue() { return this.value; } @Override public long longValue() { return this.value; } @Override public float floatValue() { return this.value; } @Override public double doubleValue() { return this.value; } @Override public BigInteger integerValue() { return BigInteger.valueOf(this.value); } @Override public Number numberValue() { return Integer.valueOf(this.value); } @Override public char charValue() { return (char) this.value; } @Override public boolean booleanValue() { return this.value != 0; } @Override public Value bitwiseOr(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value | ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value | ((NumI64) that).value); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).or(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseXor(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value ^ ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value ^ ((NumI64) that).value); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).xor(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseAnd(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value & ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value & ((NumI64) that).value); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).and(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num plus(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value + ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value + ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value + ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value + ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).add(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num minus(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value - ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value - ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value - ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value - ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).subtract(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num times(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value * ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value * ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value * ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value * ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).multiply(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num divide(Num that) { if (that instanceof NumI32) { return NumF64.from((double) this.value / (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from((double) this.value / (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from((double) this.value / (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value / ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value / ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num modulo(Num that) { if (that instanceof NumI32) { return NumI32.from(this.value % ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from((long) this.value % ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value % ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value % ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).mod(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseNot() { return NumI32.from(~this.value); } @Override public Num negative() { return NumI32.from(-this.value); } @Override public Num inverse() { return NumF64.from(1.0 / (double) this.value); } @Override public Num abs() { return NumI32.from(Math.abs(this.value)); } @Override public Num ceil() { return this; } @Override public Num floor() { return this; } @Override public Num round() { return this; } @Override public Num sqrt() { return NumF64.from(Math.sqrt((double) this.value)); } @Override public Num pow(Num that) { return NumF64.from(Math.pow((double) this.value, that.doubleValue())); } @Override public void display(Output<?> output) { Format.debugInt(this.value, output); } static final int UINT32 = 1 << 0; private static NumI32 zero; private static NumI32 positiveOne; private static NumI32 negativeOne; private static HashGenCacheSet<NumI32> cache; static NumI32 zero() { if (zero == null) { zero = new NumI32(0); } return zero; } static NumI32 positiveOne() { if (positiveOne == null) { positiveOne = new NumI32(1); } return positiveOne; } static NumI32 negativeOne() { if (negativeOne == null) { negativeOne = new NumI32(-1); } return negativeOne; } public static NumI32 from(int value) { if (value == 0) { return zero(); } else if (value == 1) { return positiveOne(); } else if (value == -1) { return negativeOne(); } else { return cache().put(new NumI32(value)); } } public static NumI32 uint32(int value) { return new NumI32(value, UINT32); } static HashGenCacheSet<NumI32> cache() { if (cache == null) { int cacheSize; try { cacheSize = Integer.parseInt(System.getProperty("swim.structure.num.i32.cache.size")); } catch (NumberFormatException e) { cacheSize = 16; } cache = new HashGenCacheSet<NumI32>(cacheSize); } return cache; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/NumI64.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import swim.codec.Format; import swim.codec.Output; import swim.util.HashGenCacheSet; final class NumI64 extends Num { final long value; int flags; NumI64(long value, int flags) { this.value = value; this.flags = flags; } NumI64(long value) { this(value, 0); } @Override public boolean isUint64() { return (this.flags & UINT64) != 0; } @Override public boolean isNaN() { return false; } @Override public boolean isInfinite() { return false; } @Override public boolean isValidByte() { return (byte) this.value == this.value; } @Override public boolean isValidShort() { return (short) this.value == this.value; } @Override public boolean isValidInt() { return (int) this.value == this.value; } @Override public boolean isValidLong() { return true; } @Override public boolean isValidFloat() { return true; } @Override public boolean isValidDouble() { return true; } @Override public boolean isValidInteger() { return true; } @Override public String stringValue() { return Long.toString(this.value); } @Override public byte byteValue() { return (byte) this.value; } @Override public short shortValue() { return (short) this.value; } @Override public int intValue() { return (int) this.value; } @Override public long longValue() { return this.value; } @Override public float floatValue() { return this.value; } @Override public double doubleValue() { return this.value; } @Override public BigInteger integerValue() { return BigInteger.valueOf(this.value); } @Override public Number numberValue() { return Long.valueOf(this.value); } @Override public char charValue() { return (char) this.value; } @Override public boolean booleanValue() { return this.value != 0L; } @Override public Value bitwiseOr(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value | (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value | ((NumI64) that).value); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).or(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseXor(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value ^ (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value ^ ((NumI64) that).value); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).xor(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseAnd(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value & (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value & ((NumI64) that).value); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).and(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num plus(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value + (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value + ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value + ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value + ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).add(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num minus(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value - (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value - ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value - ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value - ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).subtract(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num times(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value * (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value * ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value * ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value * ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).multiply(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num divide(Num that) { if (that instanceof NumI32) { return NumF64.from((double) this.value / (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from((double) this.value / (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from((double) this.value / (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value / ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from((double) this.value / ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num modulo(Num that) { if (that instanceof NumI32) { return NumI64.from(this.value % (long) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumI64.from(this.value % ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF32.from((float) this.value % ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from((double) this.value % ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(BigInteger.valueOf(this.value).mod(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseNot() { return NumI64.from(~this.value); } @Override public Num negative() { return NumI64.from(-this.value); } @Override public Num inverse() { return NumF64.from(1.0 / (double) this.value); } @Override public Num abs() { return NumI64.from(Math.abs(this.value)); } @Override public Num ceil() { return this; } @Override public Num floor() { return this; } @Override public Num round() { return this; } @Override public Num sqrt() { return NumF64.from(Math.sqrt((double) this.value)); } @Override public Num pow(Num that) { return NumF64.from(Math.pow((double) this.value, that.doubleValue())); } @Override public void display(Output<?> output) { Format.debugLong(this.value, output); } static final int UINT64 = 1 << 0; private static NumI64 zero; private static NumI64 positiveOne; private static NumI64 negativeOne; private static HashGenCacheSet<NumI64> cache; static NumI64 zero() { if (zero == null) { zero = new NumI64(0L); } return zero; } static NumI64 positiveOne() { if (positiveOne == null) { positiveOne = new NumI64(1L); } return positiveOne; } static NumI64 negativeOne() { if (negativeOne == null) { negativeOne = new NumI64(-1L); } return negativeOne; } public static NumI64 from(long value) { if (value == 0L) { return zero(); } else if (value == 1L) { return positiveOne(); } else if (value == -1L) { return negativeOne(); } else { return cache().put(new NumI64(value)); } } public static NumI64 uint64(long value) { return new NumI64(value, UINT64); } static HashGenCacheSet<NumI64> cache() { if (cache == null) { int cacheSize; try { cacheSize = Integer.parseInt(System.getProperty("swim.structure.num.i64.cache.size")); } catch (NumberFormatException e) { cacheSize = 16; } cache = new HashGenCacheSet<NumI64>(cacheSize); } return cache; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/NumInt.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import swim.codec.Output; import swim.util.HashGenCacheSet; final class NumInt extends Num { final BigInteger value; NumInt(BigInteger value) { this.value = value; } @Override public boolean isNaN() { return false; } @Override public boolean isInfinite() { return false; } @Override public boolean isValidByte() { return BigInteger.valueOf(this.value.byteValue()).equals(this.value); } @Override public boolean isValidShort() { return BigInteger.valueOf(this.value.shortValue()).equals(this.value); } @Override public boolean isValidInt() { return BigInteger.valueOf(this.value.intValue()).equals(this.value); } @Override public boolean isValidLong() { return BigInteger.valueOf(this.value.longValue()).equals(this.value); } @Override public boolean isValidFloat() { return BigInteger.valueOf((long) this.value.floatValue()).equals(this.value); } @Override public boolean isValidDouble() { return BigInteger.valueOf((long) this.value.doubleValue()).equals(this.value); } @Override public boolean isValidInteger() { return true; } @Override public String stringValue() { return this.value.toString(); } @Override public byte byteValue() { return this.value.byteValue(); } @Override public short shortValue() { return this.value.shortValue(); } @Override public int intValue() { return this.value.intValue(); } @Override public long longValue() { return this.value.longValue(); } @Override public float floatValue() { return this.value.floatValue(); } @Override public double doubleValue() { return this.value.doubleValue(); } @Override public BigInteger integerValue() { return this.value; } @Override public Number numberValue() { return this.value; } @Override public char charValue() { return (char) this.value.longValue(); } @Override public boolean booleanValue() { return !BigInteger.ZERO.equals(this.value); } @Override public Value bitwiseOr(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.or(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.or(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(this.value.or(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseXor(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.xor(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.xor(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(this.value.xor(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseAnd(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.and(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.and(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return Value.absent(); } else if (that instanceof NumF64) { return Value.absent(); } else if (that instanceof NumInt) { return NumInt.from(this.value.and(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num plus(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.add(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.add(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return NumF64.from(this.value.doubleValue() + (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value.doubleValue() + ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(this.value.add(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num minus(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.subtract(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.subtract(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return NumF64.from(this.value.doubleValue() - (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value.doubleValue() - ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(this.value.subtract(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num times(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.multiply(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.multiply(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return NumF64.from(this.value.doubleValue() * (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value.doubleValue() * ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(this.value.multiply(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Num divide(Num that) { if (that instanceof NumI32) { return NumF64.from(this.value.doubleValue() / (double) ((NumI32) that).value); } else if (that instanceof NumI64) { return NumF64.from(this.value.doubleValue() / (double) ((NumI64) that).value); } else if (that instanceof NumF32) { return NumF64.from(this.value.doubleValue() / (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value.doubleValue() / ((NumF64) that).value); } else if (that instanceof NumInt) { return NumF64.from(this.value.doubleValue() / ((NumInt) that).value.doubleValue()); } else { throw new AssertionError(); } } @Override public Num modulo(Num that) { if (that instanceof NumI32) { return NumInt.from(this.value.mod(BigInteger.valueOf(((NumI32) that).value))); } else if (that instanceof NumI64) { return NumInt.from(this.value.mod(BigInteger.valueOf(((NumI64) that).value))); } else if (that instanceof NumF32) { return NumF64.from(this.value.doubleValue() % (double) ((NumF32) that).value); } else if (that instanceof NumF64) { return NumF64.from(this.value.doubleValue() % ((NumF64) that).value); } else if (that instanceof NumInt) { return NumInt.from(this.value.mod(((NumInt) that).value)); } else { throw new AssertionError(); } } @Override public Value bitwiseNot() { return NumInt.from(this.value.not()); } @Override public Num negative() { return NumInt.from(this.value.negate()); } @Override public Num inverse() { return NumF64.from(1.0 / this.value.doubleValue()); } @Override public Num abs() { return NumInt.from(this.value.abs()); } @Override public Num ceil() { return this; } @Override public Num floor() { return this; } @Override public Num round() { return this; } @Override public Num sqrt() { return NumF64.from(Math.sqrt(this.value.doubleValue())); } @Override public Num pow(Num that) { return NumF64.from(Math.pow(this.value.doubleValue(), that.doubleValue())); } @Override public void display(Output<?> output) { output.debug(this.value.toString()); } private static NumInt zero; private static NumInt positiveOne; private static NumInt negativeOne; private static HashGenCacheSet<NumInt> cache; static NumInt zero() { if (zero == null) { zero = new NumInt(BigInteger.ZERO); } return zero; } static NumInt positiveOne() { if (positiveOne == null) { positiveOne = new NumInt(BigInteger.ONE); } return positiveOne; } static NumInt negativeOne() { if (negativeOne == null) { negativeOne = new NumInt(BigInteger.ONE.negate()); } return negativeOne; } public static NumInt from(BigInteger value) { final double doubleValue = value.doubleValue(); if (doubleValue == 0.0) { return zero(); } else if (doubleValue == 1.0) { return positiveOne(); } else if (doubleValue == -1.0) { return negativeOne(); } else { return cache().put(new NumInt(value)); } } static HashGenCacheSet<NumInt> cache() { if (cache == null) { int cacheSize; try { cacheSize = Integer.parseInt(System.getProperty("swim.structure.num.int.cache.size")); } catch (NumberFormatException e) { cacheSize = 16; } cache = new HashGenCacheSet<NumInt>(cacheSize); } return cache; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Operator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; /** * An {@link Expression} that identifies an operation on constants, variables, * or {@link Selector} expressions. */ public abstract class Operator extends Expression { @Override public abstract Item evaluate(Interpreter interpreter); @Override public int compareTo(Item other) { if (other instanceof Operator) { return compareTo((Operator) other); } return Integer.compare(typeOrder(), other.typeOrder()); } protected abstract int compareTo(Operator that); @Override public abstract boolean equals(Object other); @Override public abstract int hashCode(); }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Record.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.reflect.Array; import java.util.Collection; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; import swim.codec.Output; import swim.util.Builder; public abstract class Record extends Value implements List<Item>, Builder<Item, Record> { volatile int flags; protected Record() { // stub } /** * Returns {@code true} if this {@code Record} has no members. */ @Override public abstract boolean isEmpty(); /** * Returns {@code true} if this {@code Record} has only {@link Value} * members–no {@code Field} members. */ public boolean isArray() { return fieldCount() == 0; } /** * Returns {@code true} if this {@code Record} has only {@link Field} * members–no {@code Value} members. */ public boolean isObject() { return valueCount() == 0; } /** * Returns the number of members contained in this {@code Record}. */ @Override public abstract int size(); /** * Returns the number of members contained in this {@code Record}; equivalent * to {@link #size()}. */ @Override public final int length() { return size(); } /** * Returns the number of {@link Field} members contained in this * {@code Record}. */ public int fieldCount() { int count = 0; for (Item member : this) { if (member instanceof Field) { count += 1; } } return count; } /** * Returns the number of {@link Value} members contained in this * {@code Record}. */ public int valueCount() { int count = 0; for (Item member : this) { if (member instanceof Value) { count += 1; } } return count; } @Override public boolean isConstant() { for (Item member : this) { if (!member.isConstant()) { return false; } } return true; } /** * Returns the {@code key} string of the first member of this {@code Record}, * if the first member is an {@link Attr}; otherwise returns {@code null} if * the first member is not an {@code Attr}. * <p> * Used to concisely get the name of the discriminating attribute of a * structure. The {@code tag} can be used to discern the nominal type of a * polymorphic structure, similar to an XML element tag. */ @Override public String tag() { final Item item = head(); if (item instanceof Attr) { return ((Attr) item).key.value; } return null; } /** * Returns the {@link #flattened() flattened} members of this {@code Record} * after all attributes have been removed. * <p> * Used to concisely get the scalar value of an attributed structure. * An attributed structure is a {@code Record} with one or more attributes * that modify one or more other members. */ @Override public Value target() { Value value = null; Record record = null; boolean modified = false; for (Item item : this) { if (item instanceof Attr) { modified = true; } else if (value == null && item instanceof Value) { value = (Value) item; } else { if (record == null) { record = Record.create(); if (value != null) { record.add(value); } } record.add(item); } } if (value == null) { return Value.extant(); } else if (record == null) { return value; } else if (modified) { return record; } else { return this; } } /** * Returns the sole member of this {@code Record}, if this {@code Record} has * exactly one member, and its member is a {@code Value}; returns {@link * Extant} if this {@code Record} is empty; otherwise returns {@code this} if * this {@code Record} has more than one member. * <p> * Used to convert a unary {@code Record} into its member {@code Value}. * Facilitates writing code that treats a unary {@code Record} equivalently * to a bare {@code Value}. */ @Override public Value flattened() { if (isEmpty()) { return Value.extant(); } else { final Iterator<Item> items = iterator(); final Item head = items.next(); if (!items.hasNext() && head instanceof Value) { return (Value) head; } else { return branch(); } } } /** * Returns {@code this} {@code Record}. */ @Override public Record unflattened() { return this; } /** * Returns the value of the first member of this {@code Record}, if the first * member is an {@link Attr} whose {@code key} string is equal to {@code tag}; * otherwise returns {@link Absent} if the first member of this {@code Record} * is not an {@code Attr}, or if the first member of this {@code Record} is an * {@code Attr} whose {@code key} does not equal the {@code tag}. * <p> * Used to conditionally get the value of the head {@code Attr} of a * structure, if and only if the key string of the head {@code Attr} is equal * to the {@code tag}. Can be used to check if a structure might conform to * a nominal type named {@code tag}, while simultaneously getting the value * of the {@code tag} attribute. */ @Override public Value header(String tag) { final Item head = head(); if (head instanceof Attr && head.keyEquals(tag)) { return ((Attr) head).value; } else { return Value.absent(); } } /** * Returns the {@link #unflattened() unflattened} {@link #header(String) * header} of this {@code Record}. The {@code headers} of the {@code tag} * attribute of a structure are like the attributes of an XML element tag; * through unlike an XML element, {@code tag} attribute headers are not * limited to string keys and values. */ @Override public Record headers(String tag) { final Item head = head(); if (head instanceof Attr && head.keyEquals(tag)) { final Value header = ((Attr) head).value; if (header instanceof Record) { return (Record) header; } else { return Record.of(header); } } return null; } /** * Returns the first member of this {@code Record}, if this {@code Record} is * non-empty; otherwise returns {@link Absent}. */ @Override public Item head() { return getItem(0); } /** * Returns a view of all but the first member of this {@code Record}, if this * {@code Record} is non-empty; otherwise returns an empty {@code Record}, if * this {@code Record} is itself empty. */ @Override public Record tail() { final Record tail = Record.create(); final Iterator<Item> items = iterator(); if (items.hasNext()) { items.next(); // skip head } while (items.hasNext()) { tail.add(items.next()); } return tail; } /** * Returns the {@link #flattened() flattened} {@link #tail() tail} of this * {@code Record}. Used to recursively deconstruct a structure, terminating * with its last {@code Value}, rather than a unary {@code Record} containing * its last value, if the structure ends with a {@code Value} member. */ @Override public Value body() { final Record tail = tail(); if (!tail.isEmpty()) { return tail.flattened(); } else { return Value.absent(); } } /** * Returns {@code true} if this {@code Record} has a member equal to {@code * Item.fromObject(item)}; otherwise returns {@code false} if this {@code * Record} has no member equal to {@code Item.fromObject(item)}. */ @Override public boolean contains(Object item) { return contains(Item.fromObject(item)); } /** * Returns {@code true} if this {@code Record} has a member equal to {@code * item}; otherwise returns {@code false} if this {@code Record} has no * member equal to {@code item}. */ @Override public boolean contains(Item item) { for (Item member : this) { if (member.equals(item)) { return true; } } return false; } /** * Returns {@code true} if this {@code Record} has a member equal to every * item in {@code items}; returns {@code false} if any item in {@code items} * is not contained in this {@code Record}. */ @Override public boolean containsAll(Collection<?> items) { final HashSet<Object> q = new HashSet<Object>(items); final Iterator<Item> elems = iterator(); while (elems.hasNext() && !q.isEmpty()) { q.remove(elems.next()); } return q.isEmpty(); } /** * Returns {@code true} if this {@code Record} has a {@link Field} member * with a key that is equal to the given {@code key}; otherwise returns * {@code false} if this {@code Record} has no {@code Field} member with a * key equal to the given {@code key}. */ @Override public boolean containsKey(Value key) { for (Item item : this) { if (item instanceof Field && item.keyEquals(key)) { return true; } } return false; } /** * Returns {@code true} if this {@code Record} has a {@link Field} member * with a key that is equal to the given {@code key}; otherwise returns * {@code false} if this {@code Record} has no {@code Field} member with a * key equal to the given {@code key}. Equivalent to {@link * #containsKey(Value)}, but avoids boxing the {@code key} string into a * {@code Text} value. */ @Override public boolean containsKey(String key) { for (Item item : this) { if (item instanceof Field && item.keyEquals(key)) { return true; } } return false; } /** * Returns {@code true} if this {@code Record} has a {@link Field} member * with a value that is equal to the given {@code value}; otherwise returns * {@code false} if this {@code Record} has no {@code Field} member with a * value equal to the given {@code value}. */ @Override public boolean containsValue(Value value) { for (Item item : this) { if (item instanceof Field && item.toValue().equals(value)) { return true; } } return false; } @Override public int indexOf(Object item) { return indexOf(Item.fromObject(item)); } private int indexOf(Item item) { final Iterator<Item> items = iterator(); int index = 0; while (items.hasNext()) { if (item.equals(items.next())) { return index; } index += 1; } return -1; } @Override public int lastIndexOf(Object item) { return lastIndexOf(Item.fromObject(item)); } private int lastIndexOf(Item item) { int index = this.size() - 1; while (index >= 0) { if (item.equals(getItem(index))) { return index; } index -= 1; } return -1; } /** * Returns the value of the last {@link Field} member of this {@code Record} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Record} has no {@code Field} member with a key equal to the * given {@code key}. */ @Override public Value get(Value key) { for (Item item : this) { if (item instanceof Field && item.keyEquals(key)) { return item.toValue(); } } return Value.absent(); } /** * Returns the value of the last {@link Field} member of this {@code Record} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Record} has no {@code Field} member with a key equal to the * given {@code key}. Equivalent to {@link #get(Value)}, but avoids boxing * the {@code key} string into a {@code Text} value. */ @Override public Value get(String key) { for (Item item : this) { if (item instanceof Field && item.keyEquals(key)) { return item.toValue(); } } return Value.absent(); } /** * Returns the value of the last {@link Attr} member of this {@code Record} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Record} has no {@code Attr} member with a key equal to the * given {@code key}. */ @Override public Value getAttr(Text key) { for (Item item : this) { if (item instanceof Attr && item.keyEquals(key)) { return item.toValue(); } } return Value.absent(); } /** * Returns the value of the last {@link Attr} member of this {@code Record} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Record} has no {@code Attr} member with a key equal to the * given {@code key}. Equivalent to {@link #getAttr(Text)}, but avoids * boxing the {@code key} string into a {@code Text} value. */ @Override public Value getAttr(String key) { for (Item item : this) { if (item instanceof Attr && item.keyEquals(key)) { return item.toValue(); } } return Value.absent(); } /** * Returns the value of the last {@link Slot} member of this {@code Record} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Record} has no {@code Slot} member with a key equal to the * given {@code key}. */ @Override public Value getSlot(Value key) { for (Item item : this) { if (item instanceof Slot && item.keyEquals(key)) { return item.toValue(); } } return Value.absent(); } /** * Returns the value of the last {@link Slot} member of this {@code Record} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Record} has no {@code Slot} member with a key equal to the * given {@code key}. Equivalent to {@link #getSlot(Value)}, but avoids * boxing the {@code key} string into a {@code Text} value. */ @Override public Value getSlot(String key) { for (Item item : this) { if (item instanceof Slot && item.keyEquals(key)) { return item.toValue(); } } return Value.absent(); } /** * Returns the last {@link Field} member of this {@code Record} whose key * is equal to the given {@code key}; returns {@code null} if this {@code * Record} has no {@code Field} member with a {@code key} equal to the * given {@code key}. */ @Override public Field getField(Value key) { for (Item item : this) { if (item instanceof Field && item.keyEquals(key)) { return (Field) item; } } return null; } /** * Returns the last {@link Field} member of this {@code Record} whose key * is equal to the given {@code key}; returns {@code null} if this {@code * Record} has no {@code Field} member with a {@code key} equal to the * given {@code key}. Equivalent to {@link #getField(Value)}, but avoids * boxing the {@code key} string into a {@code Text} value. */ @Override public Field getField(String key) { for (Item item : this) { if (item instanceof Field && item.keyEquals(key)) { return (Field) item; } } return null; } /** * Returns the member of this {@code Record} at the given {@code index}, * if the {@code index} is greater than or equal to zero, and less than the * {@link #length() length} of this {@code Record}. * * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ @Override public abstract Item get(int index); /** * Returns the member of this {@code Record} at the given {@code index}, * if the {@code index} is greater than or equal to zero, and less than the * {@link #length() length} of this {@code Record}; otherwise returns {@link * Absent} if the {@code index} is out of bounds. */ @Override public abstract Item getItem(int index); public Value put(Value key, Value newValue) { final ListIterator<Item> items = listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item instanceof Field && item.keyEquals(key)) { final Field field = (Field) item; if (field.isMutable()) { return field.setValue(newValue); } else { final Value oldValue = field.toValue(); items.set(field.updatedValue(newValue)); return oldValue; } } } add(new Slot(key, newValue)); return Value.absent(); } public Value put(Value key, String newValue) { return put(key, Text.from(newValue)); } public Value put(Value key, int newValue) { return put(key, Num.from(newValue)); } public Value put(Value key, long newValue) { return put(key, Num.from(newValue)); } public Value put(Value key, float newValue) { return put(key, Num.from(newValue)); } public Value put(Value key, double newValue) { return put(key, Num.from(newValue)); } public Value put(Value key, boolean newValue) { return put(key, Bool.from(newValue)); } public Value put(String key, Value newValue) { final ListIterator<Item> items = listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item instanceof Field && item.keyEquals(key)) { final Field field = (Field) item; if (field.isMutable()) { return field.setValue(newValue); } else { final Value oldValue = field.toValue(); items.set(field.updatedValue(newValue)); return oldValue; } } } add(new Slot(Text.from(key), newValue)); return Value.absent(); } public Value put(String key, String newValue) { return put(key, Text.from(newValue)); } public Value put(String key, int newValue) { return put(key, Num.from(newValue)); } public Value put(String key, long newValue) { return put(key, Num.from(newValue)); } public Value put(String key, float newValue) { return put(key, Num.from(newValue)); } public Value put(String key, double newValue) { return put(key, Num.from(newValue)); } public Value put(String key, boolean newValue) { return put(key, Bool.from(newValue)); } public Value putAttr(Text key, Value newValue) { final ListIterator<Item> items = listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item instanceof Field && item.keyEquals(key)) { if (item instanceof Attr && item.isMutable()) { return ((Attr) item).setValue(newValue); } else { final Value oldValue = item.toValue(); items.set(new Attr(key, newValue)); return oldValue; } } } add(new Attr(key, newValue)); return Value.absent(); } public Value putAttr(Text key, String newValue) { return putAttr(key, Text.from(newValue)); } public Value putAttr(Text key, int newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(Text key, long newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(Text key, float newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(Text key, double newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(Text key, boolean newValue) { return putAttr(key, Bool.from(newValue)); } public Value putAttr(String key, Value newValue) { final ListIterator<Item> items = listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item instanceof Field && item.keyEquals(key)) { if (item instanceof Attr && item.isMutable()) { return ((Attr) item).setValue(newValue); } else { final Value oldValue = item.toValue(); items.set(new Attr(Text.from(key), newValue)); return oldValue; } } } add(new Attr(Text.from(key), newValue)); return Value.absent(); } public Value putAttr(String key, String newValue) { return putAttr(key, Text.from(newValue)); } public Value putAttr(String key, int newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(String key, long newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(String key, float newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(String key, double newValue) { return putAttr(key, Num.from(newValue)); } public Value putAttr(String key, boolean newValue) { return putAttr(key, Bool.from(newValue)); } public Value putSlot(Value key, Value newValue) { final ListIterator<Item> items = listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item instanceof Field && item.keyEquals(key)) { if (item instanceof Slot && item.isMutable()) { return ((Slot) item).setValue(newValue); } else { final Value oldValue = item.toValue(); items.set(new Slot(key, newValue)); return oldValue; } } } add(new Slot(key, newValue)); return Value.absent(); } public Value putSlot(Value key, String newValue) { return putSlot(key, Text.from(newValue)); } public Value putSlot(Value key, int newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(Value key, long newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(Value key, float newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(Value key, double newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(Value key, boolean newValue) { return putSlot(key, Bool.from(newValue)); } public Value putSlot(String key, Value newValue) { final ListIterator<Item> items = listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item instanceof Field && item.keyEquals(key)) { if (item instanceof Slot && item.isMutable()) { return ((Slot) item).setValue(newValue); } else { final Value oldValue = item.toValue(); items.set(new Slot(Text.from(key), newValue)); return oldValue; } } } add(new Slot(Text.from(key), newValue)); return Value.absent(); } public Value putSlot(String key, String newValue) { return putSlot(key, Text.from(newValue)); } public Value putSlot(String key, int newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(String key, long newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(String key, float newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(String key, double newValue) { return putSlot(key, Num.from(newValue)); } public Value putSlot(String key, boolean newValue) { return putSlot(key, Bool.from(newValue)); } public void putAll(Map<? extends Value, ? extends Value> fields) { for (Map.Entry<? extends Value, ? extends Value> field : fields.entrySet()) { put(field.getKey(), field.getValue()); } } /** * Replaces the member of this {@code Record} at the given {@code index} with * a new {@code item}, returning the previous {@code Item} at the given {@code * index}, if the {@code index} is greater than or equal to zero, and less * than the {@link #length() length} of this {@code Record}. Equivalent to * {@link #setItem(int, Item)}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ @Override public Item set(int index, Item item) { return setItem(index, item); } /** * Replaces the member of this {@code Record} at the given {@code index} with * a new {@code item}, returning the previous {@code Item} at the given {@code * index}, if the {@code index} is greater than or equal to zero, and less * than the {@link #length() length} of this {@code Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public abstract Item setItem(int index, Item item); /** * Replaces the member of this {@code Record} at the given {@code index} with * a {@link Text#from(String) Text} {@code value}, returning the previous * {@code Item} at the given {@code index}, if the {@code index} is greater * than or equal to zero, and less than the {@link #length() length} of this * {@code Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public Item setItem(int index, String value) { return setItem(index, Text.from(value)); } /** * Replaces the member of this {@code Record} at the given {@code index} with * a {@link Num#from(int) Num} {@code value}, returning the previous {@code * Item} at the given {@code index}, if the {@code index} is greater than or * equal to zero, and less than the {@link #length() length} of this {@code * Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public Item setItem(int index, int value) { return setItem(index, Num.from(value)); } /** * Replaces the member of this {@code Record} at the given {@code index} with * a {@link Num#from(long) Num} {@code value}, returning the previous * {@code Item} at the given {@code index}, if the {@code index} is greater * than or equal to zero, and less than the {@link #length() length} of this * {@code Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public Item setItem(int index, long value) { return setItem(index, Num.from(value)); } /** * Replaces the member of this {@code Record} at the given {@code index} with * a {@link Num#from(float) Num} {@code value}, returning the previous {@code * Item} at the given {@code index}, if the {@code index} is greater than or * equal to zero, and less than the {@link #length() length} of this {@code * Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public Item setItem(int index, float value) { return setItem(index, Num.from(value)); } /** * Replaces the member of this {@code Record} at the given {@code index} with * a {@link Num#from(double) Num} {@code value}, returning the previous * {@code Item} at the given {@code index}, if the {@code index} is greater * than or equal to zero, and less than the {@link #length() length} of this * {@code Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public Item setItem(int index, double value) { return setItem(index, Num.from(value)); } /** * Replaces the member of this {@code Record} at the given {@code index} with * a {@link Bool#from(boolean) Bool} {@code value}, returning the previous * {@code Item} at the given {@code index}, if the {@code index} is greater * than or equal to zero, and less than the {@link #length() length} of this * {@code Record}. * * @throws UnsupportedOperationException if this is an immutable {@code Record}. * @throws IndexOutOfBoundsException if the {@code index} is out of bounds. */ public Item setItem(int index, boolean value) { return setItem(index, Bool.from(value)); } @Override public abstract boolean add(Item item); public boolean add(String item) { return add(Text.from(item)); } public boolean add(int item) { return add(Num.from(item)); } public boolean add(long item) { return add(Num.from(item)); } public boolean add(float item) { return add(Num.from(item)); } public boolean add(double item) { return add(Num.from(item)); } public boolean add(boolean item) { return add(Bool.from(item)); } @Override public abstract void add(int index, Item item); public void add(int index, String item) { add(index, Text.from(item)); } public void add(int index, int item) { add(index, Num.from(item)); } public void add(int index, long item) { add(index, Num.from(item)); } public void add(int index, float item) { add(index, Num.from(item)); } public void add(int index, double item) { add(index, Num.from(item)); } public void add(int index, boolean item) { add(index, Bool.from(item)); } @Override public boolean addAll(Collection<? extends Item> items) { boolean changed = false; for (Item item : items) { add(item); changed = true; } return changed; } @Override public boolean addAll(int index, Collection<? extends Item> items) { boolean changed = false; for (Item item : items) { add(index, item); changed = true; index += 1; } return changed; } public Record attr(Text key, Value value) { add(new Attr(key, value)); return this; } public Record attr(Text key, String value) { return attr(key, Text.from(value)); } public Record attr(Text key, int value) { return attr(key, Num.from(value)); } public Record attr(Text key, long value) { return attr(key, Num.from(value)); } public Record attr(Text key, float value) { return attr(key, Num.from(value)); } public Record attr(Text key, double value) { return attr(key, Num.from(value)); } public Record attr(Text key, boolean value) { return attr(key, Bool.from(value)); } public Record attr(Text key) { return attr(key, Value.extant()); } public Record attr(String key, Value value) { return attr(Text.from(key), value); } public Record attr(String key, String value) { return attr(key, Text.from(value)); } public Record attr(String key, int value) { return attr(key, Num.from(value)); } public Record attr(String key, long value) { return attr(key, Num.from(value)); } public Record attr(String key, float value) { return attr(key, Num.from(value)); } public Record attr(String key, double value) { return attr(key, Num.from(value)); } public Record attr(String key, boolean value) { return attr(key, Bool.from(value)); } public Record attr(String key) { return attr(key, Value.extant()); } public Record slot(Value key, Value value) { add(new Slot(key, value)); return this; } public Record slot(Value key, String value) { return slot(key, Text.from(value)); } public Record slot(Value key, int value) { return slot(key, Num.from(value)); } public Record slot(Value key, long value) { return slot(key, Num.from(value)); } public Record slot(Value key, float value) { return slot(key, Num.from(value)); } public Record slot(Value key, double value) { return slot(key, Num.from(value)); } public Record slot(Value key, boolean value) { return slot(key, Bool.from(value)); } public Record slot(Value key) { return slot(key, Value.extant()); } public Record slot(String key, Value value) { return slot(Text.from(key), value); } public Record slot(String key, String value) { return slot(key, Text.from(value)); } public Record slot(String key, int value) { return slot(key, Num.from(value)); } public Record slot(String key, long value) { return slot(key, Num.from(value)); } public Record slot(String key, float value) { return slot(key, Num.from(value)); } public Record slot(String key, double value) { return slot(key, Num.from(value)); } public Record slot(String key, boolean value) { return slot(key, Bool.from(value)); } public Record slot(String key) { return slot(key, Value.extant()); } public Record item(Item item) { add(item); return this; } public Record item(String item) { return item(Text.from(item)); } public Record item(int item) { return item(Num.from(item)); } public Record item(long item) { return item(Num.from(item)); } public Record item(float item) { return item(Num.from(item)); } public Record item(double item) { return item(Num.from(item)); } public Record item(boolean item) { return item(Bool.from(item)); } @Override public abstract Item remove(int index); @Override public boolean remove(Object object) { final Item item = Item.fromObject(object); final int index = indexOf(item); if (index >= 0) { remove(index); return true; } else { return false; } } public boolean removeKey(Value key) { final Iterator<Item> items = iterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { items.remove(); return true; } } return false; } public boolean removeKey(String key) { final Iterator<Item> items = iterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { items.remove(); return true; } } return false; } @Override public boolean removeAll(Collection<?> items) { boolean modified = false; final Iterator<Item> iter = iterator(); while (iter.hasNext()) { final Item item = iter.next(); if (items.contains(item)) { iter.remove(); modified = true; } } return modified; } @Override public boolean retainAll(Collection<?> items) { boolean modified = false; final Iterator<Item> iter = iterator(); while (iter.hasNext()) { final Item item = iter.next(); if (!items.contains(item)) { iter.remove(); modified = true; } } return modified; } @Override public abstract void clear(); @Override public Record updated(Value key, Value value) { final Record record = isMutable() ? this : branch(); final ListIterator<Item> items = record.listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { if (item instanceof Field && item.isMutable()) { ((Field) item).setValue(value); } else { items.set(new Slot(key, value)); } return record; } } record.add(new Slot(key, value)); return record; } @Override public Record updated(String key, Value value) { final Record record = isMutable() ? this : branch(); final ListIterator<Item> items = record.listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { if (item instanceof Field && item.isMutable()) { ((Field) item).setValue(value); } else { items.set(new Slot(Text.from(key), value)); } return record; } } record.add(new Slot(Text.from(key), value)); return record; } @Override public Record updatedAttr(Text key, Value value) { final Record record = isMutable() ? this : branch(); final ListIterator<Item> items = record.listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { if (item instanceof Attr && item.isMutable()) { ((Attr) item).setValue(value); } else { items.set(new Attr(key, value)); } return record; } } record.add(new Attr(key, value)); return record; } @Override public Record updatedAttr(String key, Value value) { final Record record = isMutable() ? this : branch(); final ListIterator<Item> items = record.listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { if (item instanceof Attr && item.isMutable()) { ((Attr) item).setValue(value); } else { items.set(new Attr(Text.from(key), value)); } return record; } } record.add(new Attr(Text.from(key), value)); return record; } @Override public Record updatedSlot(Value key, Value value) { final Record record = isMutable() ? this : branch(); final ListIterator<Item> items = record.listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { if (item instanceof Slot && item.isMutable()) { ((Slot) item).setValue(value); } else { items.set(new Slot(key, value)); } return record; } } record.add(new Slot(key, value)); return record; } @Override public Record updatedSlot(String key, Value value) { final Record record = isMutable() ? this : branch(); final ListIterator<Item> items = record.listIterator(); while (items.hasNext()) { final Item item = items.next(); if (item.keyEquals(key)) { if (item instanceof Slot && item.isMutable()) { ((Slot) item).setValue(value); } else { items.set(new Slot(Text.from(key), value)); } return record; } } record.add(new Slot(Text.from(key), value)); return record; } @Override public Record appended(Item item) { final Record record = isMutable() ? this : branch(); record.add(item); return record; } @Override public Record appended(Object... items) { final Record record = isMutable() ? this : branch(); record.addAll(Record.of(items)); return record; } @Override public Record prepended(Item item) { final Record record = isMutable() ? this : branch(); record.add(0, item); return record; } @Override public Record prepended(Object... items) { final Record record = isMutable() ? this : branch(); record.addAll(0, Record.of(items)); return record; } @Override public Record removed(Value key) { final Record record = isMutable() ? this : branch(); record.removeKey(key); return record; } @Override public Record removed(String key) { final Record record = isMutable() ? this : branch(); record.removeKey(key); return record; } @Override public Record concat(Item that) { if (!that.isDefined()) { return branch(); } else { final Record record = Record.create(this.length() + that.length()); record.addAll(this); if (that instanceof Record) { record.addAll((Record) that); } else { record.add(that); } return record; } } @Override public Record evaluate(Interpreter interpreter) { final Record scope = Record.create(); interpreter.pushScope(scope); boolean changed = false; for (Item oldItem : this) { final Item newItem = oldItem.evaluate(interpreter); if (newItem.isDefined()) { scope.add(newItem); } if (oldItem != newItem) { changed = true; } } interpreter.popScope(); return changed ? scope : this; } @Override public Record substitute(Interpreter interpreter) { final Record scope = Record.create(); interpreter.pushScope(scope); boolean changed = false; for (Item oldItem : this) { final Item newItem = oldItem.substitute(interpreter); if (newItem.isDefined()) { scope.add(newItem); } if (oldItem != newItem) { changed = true; } } interpreter.popScope(); return changed ? scope : this; } @Override public String stringValue() { return stringValue(null); } @Override public String stringValue(String orElse) { final StringBuilder recordString = new StringBuilder(); for (Item item : this) { if (item instanceof Value) { final String itemString = item.stringValue(null); if (itemString != null) { recordString.append(itemString); continue; } } return orElse; } return recordString.toString(); } public boolean isAliased() { return false; } public boolean isMutable() { return true; } public void alias() { // nop } @Override public Record branch() { final Record branch = Record.create(); branch.addAll(this); return branch; } @Override public Record commit() { return this; } @Override public Record bind() { return this; } @Override public Item[] toArray() { final int n = size(); final Item[] array = new Item[n]; final Iterator<Item> items = iterator(); int i = 0; while (items.hasNext()) { array[i] = items.next(); i += 1; } return array; } @SuppressWarnings("unchecked") @Override public <T> T[] toArray(T[] array) { final int n = size(); if (array.length < n) { array = (T[]) Array.newInstance(array.getClass().getComponentType(), n); } final Iterator<Item> items = iterator(); int i = 0; while (items.hasNext()) { array[i] = (T) items.next(); i += 1; } if (array.length > n) { array[n] = null; } return array; } @Override public Record subList(int fromIndex, int toIndex) { final Iterator<Item> items = iterator(); final Record record = Record.create(); int index = 0; while (items.hasNext()) { if (index >= fromIndex && index < toIndex) { record.add(items.next()); } else if (index >= toIndex) { break; } index += 1; } return record; } @SuppressWarnings("unchecked") public Set<Map.Entry<Value, Value>> entrySet() { return (Set<Map.Entry<Value, Value>>) (Set<?>) fieldSet(); } public Set<Field> fieldSet() { return new RecordFieldSet(this); } public Set<Value> keySet() { return new RecordKeySet(this); } public Collection<Value> values() { return new RecordValues(this); } @Override public Iterator<Item> iterator() { return new RecordIterator(this); } @Override public ListIterator<Item> listIterator() { return new RecordIterator(this); } @Override public ListIterator<Item> listIterator(int index) { if (index < 0 || index > size()) { throw new IndexOutOfBoundsException(Integer.toString(index)); } return new RecordIterator(this, index); } public Iterator<Value> keyIterator() { return new RecordKeyIterator(iterator()); } public Iterator<Value> valueIterator() { return new RecordValueIterator(iterator()); } public Iterator<Field> fieldIterator() { return new RecordFieldIterator(iterator()); } @Override public int typeOrder() { return 3; } @Override public int compareTo(Item other) { if (other instanceof Record) { return compareTo((Record) other); } return Integer.compare(typeOrder(), other.typeOrder()); } public int compareTo(Record that) { final Iterator<Item> xs = iterator(); final Iterator<Item> ys = that.iterator(); int order = 0; do { if (xs.hasNext() && ys.hasNext()) { order = xs.next().compareTo(ys.next()); } else { break; } } while (order == 0); if (order != 0) { return order; } else if (!xs.hasNext() && ys.hasNext()) { return -1; } else if (xs.hasNext() && !ys.hasNext()) { return 1; } else { return 0; } } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof List<?>) { final List<?> that = (List<?>) other; final Iterator<Item> xs = iterator(); final Iterator<?> ys = that.iterator(); while (xs.hasNext() && ys.hasNext()) { if (!xs.next().equals(ys.next())) { return false; } } return !xs.hasNext() && !ys.hasNext(); } return false; } @Override public int hashCode() { int code = 1; for (Item item : this) { code = 31 * code + item.hashCode(); } return code; } @Override public void debug(Output<?> output) { output = output.write("Record").write('.'); if (isEmpty()) { output = output.write("empty").write('(').write(')'); } else { final Iterator<Item> items = iterator(); output = output.write("of").write('(').display(items.next()); while (items.hasNext()) { output = output.write(", ").display(items.next()); } output = output.write(')'); } } static final int ALIASED = 1 << 0; static final int IMMUTABLE = 1 << 1; protected static final AtomicIntegerFieldUpdater<Record> FLAGS = AtomicIntegerFieldUpdater.newUpdater(Record.class, "flags"); public static Record empty() { return RecordMap.empty(); } public static Record create() { return RecordMap.create(); } public static Record create(int initialSize) { return RecordMap.create(initialSize); } public static Record of() { return RecordMap.of(); } public static Record of(Object object) { return RecordMap.of(object); } public static Record of(Object... objects) { return RecordMap.of(objects); } static int expand(int n) { n = Math.max(8, n) - 1; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; return n + 1; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordFieldIterator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.Iterator; import java.util.NoSuchElementException; final class RecordFieldIterator implements Iterator<Field> { final Iterator<Item> iterator; Field next; RecordFieldIterator(Iterator<Item> iterator) { this.iterator = iterator; } @Override public boolean hasNext() { do { Item next = this.next; if (next != null) { return true; } else if (this.iterator.hasNext()) { next = this.iterator.next(); if (next instanceof Field) { this.next = (Field) next; return true; } } else { return false; } } while (true); } @Override public Field next() { do { Item next = this.next; if (next != null) { this.next = null; return (Field) next; } else if (this.iterator.hasNext()) { next = this.iterator.next(); if (next instanceof Field) { return (Field) next; } } else { throw new NoSuchElementException(); } } while (true); } @Override public void remove() { this.iterator.remove(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordFieldSet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.AbstractSet; import java.util.Iterator; final class RecordFieldSet extends AbstractSet<Field> { final Record record; RecordFieldSet(Record record) { this.record = record; } @Override public int size() { return this.record.fieldCount(); } @Override public Iterator<Field> iterator() { return this.record.fieldIterator(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordIterator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.ListIterator; import java.util.NoSuchElementException; final class RecordIterator implements ListIterator<Item> { final Record record; int index; final int fromIndex; final int toIndex; int direction; RecordIterator(Record record, int index, int fromIndex, int toIndex) { this.record = record; this.index = index; this.fromIndex = fromIndex; this.toIndex = toIndex; this.direction = 0; } RecordIterator(Record record, int index) { this.record = record; this.index = index; this.fromIndex = 0; this.toIndex = record.size(); this.direction = 0; } RecordIterator(Record record) { this.record = record; this.index = 0; this.fromIndex = 0; this.toIndex = record.size(); this.direction = 0; } @Override public boolean hasNext() { return this.index < this.toIndex; } @Override public int nextIndex() { return this.index - this.fromIndex; } @Override public Item next() { final int i = this.index; if (i < this.fromIndex || i >= this.toIndex) { throw new NoSuchElementException(); } final Item item = this.record.get(i); this.index = i + 1; this.direction = 1; return item; } @Override public boolean hasPrevious() { return this.index > this.fromIndex; } @Override public int previousIndex() { return this.index - this.fromIndex - 1; } @Override public Item previous() { final int i = this.index - 1; if (i < this.fromIndex || i >= this.toIndex) { throw new NoSuchElementException(); } this.index = i; this.direction = -1; return this.record.get(i); } @Override public void add(Item newItem) { final int i = this.index; this.record.add(i, newItem); this.index = i + 1; this.direction = 0; } @Override public void set(Item newItem) { if (direction == 0) { throw new IllegalStateException(); } if (direction > 0) { this.record.set(index - 1, newItem); } else { this.record.set(index, newItem); } } @Override public void remove() { if (direction == 0) { throw new IllegalStateException(); } if (direction > 0) { this.index -= 1; } this.record.remove(this.index); this.direction = 0; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordKeyIterator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.Iterator; import java.util.NoSuchElementException; final class RecordKeyIterator implements Iterator<Value> { final Iterator<Item> iterator; Value next; RecordKeyIterator(Iterator<Item> iterator) { this.iterator = iterator; } @Override public boolean hasNext() { do { Item next = this.next; if (next != null) { return true; } else if (this.iterator.hasNext()) { next = this.iterator.next(); if (next instanceof Field) { this.next = next.key(); return true; } } else { return false; } } while (true); } @Override public Value next() { do { Item next = this.next; if (next != null) { this.next = null; return (Value) next; } else if (this.iterator.hasNext()) { next = this.iterator.next(); if (next instanceof Field) { return next.key(); } } else { throw new NoSuchElementException(); } } while (true); } @Override public void remove() { this.iterator.remove(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordKeySet.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.AbstractSet; import java.util.Iterator; final class RecordKeySet extends AbstractSet<Value> { final Record record; RecordKeySet(Record record) { this.record = record; } @Override public int size() { return this.record.fieldCount(); } @Override public Iterator<Value> iterator() { return this.record.keyIterator(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordMap.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.reflect.Array; import java.util.Collection; import java.util.HashSet; final class RecordMap extends Record { Item[] array; Field[] table; int itemCount; int fieldCount; RecordMap(Item[] array, Field[] table, int itemCount, int fieldCount, int flags) { this.array = array; this.table = table; this.itemCount = itemCount; this.fieldCount = fieldCount; this.flags = flags; } @Override public boolean isEmpty() { return this.itemCount == 0; } @Override public int size() { return this.itemCount; } @Override public int fieldCount() { return this.fieldCount; } @Override public int valueCount() { return this.itemCount - this.fieldCount; } @Override public boolean isConstant() { final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { if (!array[i].isConstant()) { return false; } } return true; } @Override public String tag() { if (this.fieldCount > 0) { final Item item = this.array[0]; if (item instanceof Attr) { return ((Attr) item).key.value; } } return null; } @Override public Value target() { Value value = null; Record record = null; boolean modified = false; final Item[] array = this.array; final int n = this.itemCount; for (int i = 0; i < n; i += 1) { final Item item = array[i]; if (item instanceof Attr) { modified = true; } else if (value == null && item instanceof Value) { value = (Value) item; } else { if (record == null) { record = Record.create(); if (value != null) { record.add(value); } } record.add(item); } } if (value == null) { return Value.extant(); } else if (record == null) { return value; } else if (modified) { return record; } else { return this; } } @Override public Item head() { if (this.itemCount > 0) { return this.array[0]; } else { return Item.absent(); } } @Override public Record tail() { final int n = this.itemCount; if (n > 0) { return new RecordMapView(this, 1, n); } else { return Record.empty(); } } @Override public Value body() { final int n = this.itemCount; if (n > 2) { return new RecordMapView(this, 1, n).branch(); } else if (n == 2) { final Item item = this.array[1]; if (item instanceof Value) { return (Value) item; } else { return Record.of(item); } } else { return Value.absent(); } } @Override public boolean contains(Item item) { final Item[] array = this.array; final int n = this.itemCount; for (int i = 0; i < n; i += 1) { if (array[i].equals(item)) { return true; } } return false; } @Override public boolean containsAll(Collection<?> items) { final HashSet<Object> q = new HashSet<Object>(items); final Item[] array = this.array; final int n = this.itemCount; for (int i = 0; i < n && !q.isEmpty(); i += 1) { q.remove(array[i]); } return q.isEmpty(); } @Override public boolean containsKey(Value key) { return containsKey((Object) key); } @Override public boolean containsKey(String key) { return containsKey((Object) key); } private boolean containsKey(Object key) { if (!(key instanceof Value) && !(key instanceof String)) { key = Value.fromObject(key); } if (this.fieldCount != 0) { final Field[] table = hashTable(); final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field.keyEquals(key)) { return true; } } else { break; } i = (i + 1) % n; } while (i != x); } return false; } @Override public boolean containsValue(Value value) { if (this.fieldCount != 0) { final Field[] table = hashTable(); final int n = table.length; for (int i = 0; i < n; i += 1) { final Field field = table[i]; if (field != null && field.value().equals(value)) { return true; } } } return false; } @Override public int indexOf(Object object) { final Item item = Item.fromObject(object); final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { if (array[i].equals(item)) { return i; } } return -1; } @Override public int lastIndexOf(Object object) { final Item item = Item.fromObject(object); final Item[] array = this.array; for (int i = this.itemCount - 1; i >= 0; i -= 1) { if (array[i].equals(item)) { return i; } } return -1; } @Override public Value get(Value key) { return get((Object) key); } @Override public Value get(String key) { return get((Object) key); } private Value get(Object key) { if (!(key instanceof Value) && !(key instanceof String)) { key = Value.fromObject(key); } if (this.fieldCount != 0) { final Field[] table = hashTable(); final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field.keyEquals(key)) { return field.value(); } } else { break; } i = (i + 1) % n; } while (i != x); } return Value.absent(); } @Override public Value getAttr(Text key) { return getAttr((Object) key); } @Override public Value getAttr(String key) { return getAttr((Object) key); } private Value getAttr(Object key) { if (this.fieldCount != 0) { final Field[] table = hashTable(); final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field instanceof Attr && field.keyEquals(key)) { return field.value(); } } else { break; } i = (i + 1) % n; } while (i != x); } return Value.absent(); } @Override public Value getSlot(Value key) { return getSlot((Object) key); } @Override public Value getSlot(String key) { return getSlot((Object) key); } private Value getSlot(Object key) { if (this.fieldCount != 0) { final Field[] table = hashTable(); final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field instanceof Slot && field.keyEquals(key)) { return field.value(); } } else { break; } i = (i + 1) % n; } while (i != x); } return Value.absent(); } @Override public Field getField(Value key) { return getField((Object) key); } @Override public Field getField(String key) { return getField((Object) key); } private Field getField(Object key) { if (this.fieldCount != 0) { final Field[] table = hashTable(); final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field.keyEquals(key)) { return field; } } else { break; } i = (i + 1) % n; } while (i != x); } return null; } @Override public Item get(int index) { if (index < 0 || index >= this.itemCount) { throw new IndexOutOfBoundsException(Integer.toString(index)); } return this.array[index]; } @Override public Item getItem(int index) { if (index >= 0 && index < this.itemCount) { return this.array[index]; } else { return Item.absent(); } } @Override public Value put(Value key, Value newValue) { return put((Object) key, newValue); } @Override public Value put(String key, Value newValue) { return put((Object) key, newValue); } private Value put(Object key, Value newValue) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { if (this.fieldCount > 0) { return putAliased(key, newValue); } else { addAliased(new Slot(Value.fromObject(key), newValue)); return Value.absent(); } } else { if (this.fieldCount > 0) { if (this.table != null) { return putMutable(key, newValue); } else { return updateMutable(key, newValue); } } else { addMutable(new Slot(Value.fromObject(key), newValue)); return Value.absent(); } } } private Value putAliased(Object key, Value newValue) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n + 1)]; for (int i = 0; i < n; i += 1) { final Item item = oldArray[i]; if (item instanceof Field && item.keyEquals(key)) { final Value oldValue = item.toValue(); newArray[i] = ((Field) item).updatedValue(newValue); i += 1; System.arraycopy(oldArray, i, newArray, i, n - i); this.array = newArray; table = null; this.flags &= ~ALIASED; return oldValue; } newArray[i] = item; } newArray[n] = new Slot(Value.fromObject(key), newValue); this.array = newArray; this.table = null; this.itemCount = n + 1; this.fieldCount += 1; this.flags &= ~ALIASED; return Value.absent(); } private Value putMutable(Object key, Value newValue) { final Field[] table = this.table; final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field.keyEquals(key)) { if (field.isMutable()) { return field.setValue(newValue); } else { return updateMutable(key, newValue); } } } else { break; } i = (i + 1) % n; } while (i != x); final Field field = new Slot(Value.fromObject(key), newValue); addMutable(field); RecordMap.put(table, field); return Value.absent(); } private Value updateMutable(Object key, Value newValue) { final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { final Item item = array[i]; if (item instanceof Field && item.keyEquals(key)) { final Value oldValue = item.toValue(); array[i] = ((Field) item).updatedValue(newValue); this.table = null; return oldValue; } } final Field field = new Slot(Value.fromObject(key), newValue); addMutable(field); RecordMap.put(this.table, field); return Value.absent(); } @Override public Value putAttr(Text key, Value newValue) { return putAttr((Object) key, newValue); } @Override public Value putAttr(String key, Value newValue) { return putAttr((Object) key, newValue); } private Value putAttr(Object key, Value newValue) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { if (this.fieldCount > 0) { return putAttrAliased(key, newValue); } else { addAliased(new Attr(Text.fromObject(key), newValue)); return Value.absent(); } } else { if (this.fieldCount > 0) { if (this.table != null) { return putAttrMutable(key, newValue); } else { return updateAttrMutable(key, newValue); } } else { addMutable(new Attr(Text.fromObject(key), newValue)); return Value.absent(); } } } private Value putAttrAliased(Object key, Value newValue) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n + 1)]; for (int i = 0; i < n; i += 1) { final Item item = oldArray[i]; if (item instanceof Field && item.keyEquals(key)) { final Value oldValue = item.toValue(); newArray[i] = new Attr(Text.fromObject(key), newValue); i += 1; System.arraycopy(oldArray, i, newArray, i, n - i); this.array = newArray; this.table = null; this.flags &= ~ALIASED; return oldValue; } newArray[i] = item; } newArray[n] = new Attr(Text.fromObject(key), newValue); this.array = newArray; this.table = null; this.itemCount = n + 1; this.fieldCount += 1; this.flags &= ~ALIASED; return Value.absent(); } private Value putAttrMutable(Object key, Value newValue) { final Field[] table = this.table; final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field.keyEquals(key)) { if (field instanceof Attr && field.isMutable()) { return field.setValue(newValue); } else { return updateAttrMutable(key, newValue); } } } else { break; } i = (i + 1) % n; } while (i != x); final Field field = new Attr(Text.fromObject(key), newValue); add(field); RecordMap.put(table, field); return Value.absent(); } private Value updateAttrMutable(Object key, Value newValue) { final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { final Item item = array[i]; if (item instanceof Field && item.keyEquals(key)) { final Value oldValue = item.toValue(); array[i] = new Attr(Text.fromObject(key), newValue); this.table = null; return oldValue; } } final Field field = new Attr(Text.fromObject(key), newValue); add(field); RecordMap.put(this.table, field); return Value.absent(); } @Override public Value putSlot(Value key, Value newValue) { return putSlot((Object) key, newValue); } @Override public Value putSlot(String key, Value newValue) { return putSlot((Object) key, newValue); } private Value putSlot(Object key, Value newValue) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { if (this.fieldCount > 0) { return putSlotAliased(key, newValue); } else { addAliased(new Slot(Value.fromObject(key), newValue)); return Value.absent(); } } else { if (this.fieldCount > 0) { if (this.table != null) { return putSlotMutable(key, newValue); } else { return updateSlotMutable(key, newValue); } } else { addMutable(new Slot(Value.fromObject(key), newValue)); return Value.absent(); } } } private Value putSlotAliased(Object key, Value newValue) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n + 1)]; for (int i = 0; i < n; i += 1) { final Item item = oldArray[i]; if (item instanceof Field && item.keyEquals(key)) { final Value oldValue = item.toValue(); newArray[i] = new Slot(Value.fromObject(key), newValue); i += 1; System.arraycopy(oldArray, i, newArray, i, n - i); this.array = newArray; this.table = null; this.flags &= ~ALIASED; return oldValue; } newArray[i] = item; } newArray[n] = new Slot(Value.fromObject(key), newValue); this.array = newArray; this.table = null; this.itemCount = n + 1; this.fieldCount += 1; this.flags &= ~ALIASED; return Value.absent(); } private Value putSlotMutable(Object key, Value newValue) { final Field[] table = this.table; final int n = table.length; assert n > 0; final int x = Math.abs(key.hashCode() % n); int i = x; do { final Field field = table[i]; if (field != null) { if (field.keyEquals(key)) { if (field instanceof Slot && field.isMutable()) { return field.setValue(newValue); } else { return updateSlotMutable(key, newValue); } } } else { break; } i = (i + 1) % n; } while (i != x); final Field field = new Slot(Value.fromObject(key), newValue); add(field); RecordMap.put(table, field); return Value.absent(); } private Value updateSlotMutable(Object key, Value newValue) { final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { final Item item = array[i]; if (item instanceof Field && item.keyEquals(key)) { final Value oldValue = item.toValue(); array[i] = new Slot(Value.fromObject(key), newValue); this.table = null; return oldValue; } } final Field field = new Slot(Value.fromObject(key), newValue); add(field); RecordMap.put(this.table, field); return Value.absent(); } @Override public Item setItem(int index, Item newItem) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index >= this.itemCount) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((this.flags & ALIASED) != 0) { return setItemAliased(index, newItem); } else { return setItemMutable(index, newItem); } } private Item setItemAliased(int index, Item newItem) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n)]; System.arraycopy(oldArray, 0, newArray, 0, n); final Item oldItem = oldArray[index]; newArray[index] = newItem; this.array = newArray; this.table = null; if (newItem instanceof Field) { if (!(oldItem instanceof Field)) { this.fieldCount += 1; } } else if (oldItem instanceof Field) { this.fieldCount -= 1; } this.flags &= ~ALIASED; return oldItem; } private Item setItemMutable(int index, Item newItem) { final Item[] array = this.array; final Item oldItem = array[index]; array[index] = newItem; if (newItem instanceof Field) { this.table = null; if (!(oldItem instanceof Field)) { this.fieldCount += 1; } } else if (oldItem instanceof Field) { this.table = null; this.fieldCount -= 1; } return oldItem; } @Override public Record updated(Value key, Value newValue) { return updated((Object) key, newValue); } @Override public Record updated(String key, Value newValue) { return updated((Object) key, newValue); } private Record updated(Object key, Value newValue) { final RecordMap record = (this.flags & IMMUTABLE) == 0 ? this : branch(); if ((record.flags & ALIASED) != 0) { if (record.fieldCount > 0) { record.putAliased(key, newValue); } else { record.addAliased(new Slot(Value.fromObject(key), newValue)); } } else { if (record.fieldCount > 0) { if (record.table != null) { record.putMutable(key, newValue); } else { record.updateMutable(key, newValue); } } else { record.addMutable(new Slot(Value.fromObject(key), newValue)); } } return record; } @Override public Record updatedAttr(Text key, Value newValue) { return updatedAttr((Object) key, newValue); } @Override public Record updatedAttr(String key, Value newValue) { return updatedAttr((Object) key, newValue); } private Record updatedAttr(Object key, Value newValue) { final RecordMap record = (this.flags & IMMUTABLE) == 0 ? this : branch(); if ((record.flags & ALIASED) != 0) { if (record.fieldCount > 0) { record.putAttrAliased(key, newValue); } else { record.addAliased(new Attr(Text.fromObject(key), newValue)); } } else { if (record.fieldCount > 0) { if (record.table != null) { record.putAttrMutable(key, newValue); } else { record.updateAttrMutable(key, newValue); } } else { record.addMutable(new Attr(Text.fromObject(key), newValue)); } } return record; } @Override public Record updatedSlot(Value key, Value newValue) { return updatedSlot((Object) key, newValue); } @Override public Record updatedSlot(String key, Value newValue) { return updatedSlot((Object) key, newValue); } private Record updatedSlot(Object key, Value newValue) { final RecordMap record = (this.flags & IMMUTABLE) == 0 ? this : branch(); if ((record.flags & ALIASED) != 0) { if (record.fieldCount > 0) { record.putSlotAliased(key, newValue); } else { record.addAliased(new Slot(Value.fromObject(key), newValue)); } } else { if (record.fieldCount > 0) { if (record.table != null) { record.putSlotMutable(key, newValue); } else { record.updateSlotMutable(key, newValue); } } else { record.addMutable(new Slot(Value.fromObject(key), newValue)); } } return record; } @Override public boolean add(Item newItem) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { return addAliased(newItem); } else { return addMutable(newItem); } } private boolean addAliased(Item newItem) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n + 1)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, n); } newArray[n] = newItem; this.array = newArray; this.table = null; this.itemCount = n + 1; if (newItem instanceof Field) { this.fieldCount += 1; } this.flags &= ~ALIASED; return true; } private boolean addMutable(Item newItem) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray; if (oldArray == null || n + 1 > oldArray.length) { newArray = new Item[expand(n + 1)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, n); } this.array = newArray; } else { newArray = oldArray; } newArray[n] = newItem; this.itemCount = n + 1; if (newItem instanceof Field) { this.fieldCount += 1; final Field[] table = this.table; if (table != null && Math.max(table.length, table.length * 7 / 10) > n + 1) { put(table, (Field) newItem); } else { this.table = null; } } return true; } @Override public void add(int index, Item newItem) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index > this.itemCount) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == this.itemCount) { add(newItem); } else if ((this.flags & ALIASED) != 0) { addAliased(index, newItem); } else { addMutable(index, newItem); } } private void addAliased(int index, Item newItem) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n + 1)]; System.arraycopy(oldArray, 0, newArray, 0, index); System.arraycopy(oldArray, index, newArray, index + 1, n - index); newArray[index] = newItem; this.array = newArray; this.table = null; this.itemCount = n + 1; if (newItem instanceof Field) { this.fieldCount += 1; } this.flags &= ~ALIASED; } private void addMutable(int index, Item newItem) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray; if (n + 1 > oldArray.length) { newArray = new Item[expand(n + 1)]; System.arraycopy(oldArray, 0, newArray, 0, index); } else { newArray = oldArray; } System.arraycopy(oldArray, index, newArray, index + 1, n - index); newArray[index] = newItem; this.array = newArray; this.itemCount = n + 1; if (newItem instanceof Field) { this.fieldCount += 1; this.table = null; } } @Override public boolean addAll(Collection<? extends Item> newItems) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { return addAllAliased(newItems); } else { return addAllMutable(newItems); } } private boolean addAllAliased(Collection<? extends Item> newItems) { final int k = newItems.size(); if (k == 0) { return false; } int m = this.itemCount; int n = this.fieldCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(m + k)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, m); } for (Item newItem : newItems) { newArray[m] = newItem; m += 1; if (newItem instanceof Field) { n += 1; } } this.array = newArray; this.table = null; this.itemCount = m; this.fieldCount = n; this.flags &= ~ALIASED; return true; } private boolean addAllMutable(Collection<? extends Item> newItems) { final int k = newItems.size(); if (k == 0) { return false; } int m = this.itemCount; int n = this.fieldCount; final Item[] oldArray = this.array; final Item[] newArray; if (oldArray == null || m + k > oldArray.length) { newArray = new Item[expand(m + k)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, m); } } else { newArray = oldArray; } for (Item newItem : newItems) { newArray[m] = newItem; m += 1; if (newItem instanceof Field) { n += 1; this.table = null; } } this.array = newArray; this.itemCount = m; this.fieldCount = n; return true; } @Override public boolean addAll(int index, Collection<? extends Item> newItems) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index > this.itemCount) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == this.itemCount) { return addAll(newItems); } else if ((this.flags & ALIASED) != 0) { return addAllAliased(index, newItems); } else { return addAllMutable(index, newItems); } } private boolean addAllAliased(int index, Collection<? extends Item> newItems) { final int k = newItems.size(); if (k == 0) { return false; } final int m = this.itemCount; int n = this.fieldCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(m + k)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, index); System.arraycopy(oldArray, index, newArray, index + k, m - index); } for (Item newItem : newItems) { newArray[index] = newItem; index += 1; if (newItem instanceof Field) { n += 1; } } this.array = newArray; this.table = null; this.itemCount = m + k; this.fieldCount = n; this.flags &= ~ALIASED; return true; } private boolean addAllMutable(int index, Collection<? extends Item> newItems) { final int k = newItems.size(); if (k == 0) { return false; } final int m = this.itemCount; int n = this.fieldCount; final Item[] oldArray = this.array; final Item[] newArray; if (oldArray == null || m + k > oldArray.length) { newArray = new Item[expand(m + k)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, index); } } else { newArray = oldArray; } if (oldArray != null) { System.arraycopy(oldArray, index, newArray, index + k, m - index); } for (Item newItem : newItems) { newArray[index] = newItem; index += 1; if (newItem instanceof Field) { n += 1; this.table = null; } } this.array = newArray; this.itemCount = m + k; this.fieldCount = n; return true; } @Override public Item remove(int index) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index >= this.itemCount) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((this.flags & ALIASED) != 0) { return removeAliased(index); } else { return removeMutable(index); } } private Item removeAliased(int index) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n - 1)]; final Item oldItem = oldArray[index]; System.arraycopy(oldArray, 0, newArray, 0, index); System.arraycopy(oldArray, index + 1, newArray, index, n - index - 1); this.array = newArray; this.table = null; this.itemCount = n - 1; if (oldItem instanceof Field) { this.fieldCount -= 1; } this.flags &= ~ALIASED; return oldItem; } private Item removeMutable(int index) { final int n = this.itemCount; final Item[] array = this.array; final Item oldItem = array[index]; System.arraycopy(array, index + 1, array, index, n - index - 1); array[n - 1] = null; this.itemCount = n - 1; if (oldItem instanceof Field) { this.fieldCount -= 1; this.table = null; } return oldItem; } @Override public boolean remove(Object object) { final Item item = Item.fromObject(object); if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } final int index = indexOf(item); if (index >= 0) { remove(index); return true; } else { return false; } } @Override public boolean removeKey(Value key) { return removeKey((Object) key); } @Override public boolean removeKey(String key) { return removeKey((Object) key); } private boolean removeKey(Object key) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { return removeKeyAliased(key); } else { return removeKeyMutable(key); } } private boolean removeKeyAliased(Object key) { final int n = this.itemCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(n)]; for (int i = 0; i < n; i += 1) { final Item item = oldArray[i]; if (item.keyEquals(key)) { System.arraycopy(oldArray, i + 1, newArray, i, n - i - 1); this.array = newArray; this.table = null; this.itemCount = n - 1; this.fieldCount -= 1; this.flags &= ~ALIASED; return true; } newArray[i] = item; } return false; } private boolean removeKeyMutable(Object key) { final int n = this.itemCount; final Item[] array = this.array; for (int i = 0; i < n; i += 1) { final Item item = array[i]; if (item.keyEquals(key)) { System.arraycopy(array, i + 1, array, i, n - i - 1); array[n - 1] = null; this.table = null; this.itemCount = n - 1; this.fieldCount -= 1; return true; } } return false; } @Override public boolean removeAll(Collection<?> items) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { return removeAllAliased(items); } else { return removeAllMutable(items); } } private boolean removeAllAliased(Collection<?> items) { final int m = this.itemCount; int n = this.fieldCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(m)]; int i = 0; int j = 0; while (i < m) { final Item item = oldArray[i]; if (!items.contains(item)) { newArray[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; } i += 1; } if (i > j) { this.array = newArray; this.table = null; this.itemCount = j; this.fieldCount = n; this.flags &= ~ALIASED; return true; } else { return false; } } private boolean removeAllMutable(Collection<?> items) { final int m = this.itemCount; int n = this.fieldCount; final Item[] array = this.array; int i = 0; int j = 0; while (i < m) { final Item item = array[i]; if (!items.contains(item)) { array[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; this.table = null; } i += 1; } if (i > j) { while (i > j) { i -= 1; array[i] = null; } this.itemCount = j; this.fieldCount = n; return true; } else { return false; } } @Override public boolean retainAll(Collection<?> items) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.flags & ALIASED) != 0) { return retainAllAliased(items); } else { return retainAllMutable(items); } } private boolean retainAllAliased(Collection<?> items) { final int m = this.itemCount; int n = this.fieldCount; final Item[] oldArray = this.array; final Item[] newArray = new Item[expand(m)]; int i = 0; int j = 0; while (i < m) { final Item item = oldArray[i]; if (items.contains(item)) { newArray[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; } i += 1; } if (i > j) { this.array = newArray; this.table = null; this.itemCount = j; this.fieldCount = n; this.flags &= ~ALIASED; return true; } else { return false; } } private boolean retainAllMutable(Collection<?> items) { final int m = this.itemCount; int n = this.fieldCount; final Item[] array = this.array; int i = 0; int j = 0; while (i < m) { final Item item = array[i]; if (items.contains(item)) { array[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; this.table = null; } i += 1; } if (i > j) { while (i > j) { i -= 1; array[i] = null; } this.itemCount = j; this.fieldCount = n; return true; } else { return false; } } @Override public void clear() { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } this.array = null; this.table = null; this.itemCount = 0; this.fieldCount = 0; this.flags = 0; } @Override public boolean isAliased() { return (this.flags & ALIASED) != 0; } @Override public boolean isMutable() { return (this.flags & IMMUTABLE) == 0; } @Override public void alias() { this.flags |= ALIASED; } @Override public RecordMap branch() { if ((this.flags & (ALIASED | IMMUTABLE)) == 0) { final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { array[i].alias(); } } this.flags |= ALIASED; return new RecordMap(this.array, this.table, this.itemCount, this.fieldCount, ALIASED); } @Override public RecordMap commit() { if ((this.flags & IMMUTABLE) == 0) { this.flags |= IMMUTABLE; final Item[] array = this.array; for (int i = 0, n = this.itemCount; i < n; i += 1) { array[i].commit(); } } return this; } private Field[] hashTable() { final int n = this.fieldCount; Field[] table = this.table; if (n != 0 && table == null) { table = new Field[expand(Math.max(n, n * 10 / 7))]; final int m = this.itemCount; final Item[] array = this.array; for (int i = 0; i < m; i += 1) { final Item item = array[i]; if (item instanceof Field) { put(table, (Field) item); } } this.table = table; } return table; } static void put(Field[] table, Field field) { if (table == null) { return; } final int n = table.length; final int x = Math.abs(field.getKey().hashCode() % n); int i = x; do { final Field item = table[i]; if (item != null) { if (field.keyEquals(item)) { table[i] = field; return; } } else { table[i] = field; return; } i = (i + 1) % n; } while (i != x); throw new AssertionError(); } @Override public Record evaluate(Interpreter interpreter) { final int n = this.itemCount; final Item[] array = this.array; final Record scope = Record.create(n); interpreter.pushScope(scope); boolean changed = false; for (int i = 0; i < n; i += 1) { final Item oldItem = array[i]; final Item newItem = oldItem.evaluate(interpreter); scope.add(newItem); if (oldItem != newItem) { changed = true; } } interpreter.popScope(); return changed ? scope : this; } @Override public Record substitute(Interpreter interpreter) { final int n = this.itemCount; final Item[] array = this.array; final Record scope = Record.create(n); interpreter.pushScope(scope); boolean changed = false; for (int i = 0; i < n; i += 1) { final Item oldItem = array[i]; final Item newItem = oldItem.substitute(interpreter); scope.add(newItem); if (oldItem != newItem) { changed = true; } } interpreter.popScope(); return changed ? scope : this; } @Override public Item[] toArray() { final int n = this.itemCount; final Item[] array = new Item[n]; System.arraycopy(this.array, 0, array, 0, n); return array; } @SuppressWarnings("unchecked") @Override public <T> T[] toArray(T[] array) { final int n = this.itemCount; if (array.length < n) { array = (T[]) Array.newInstance(array.getClass().getComponentType(), n); } System.arraycopy(this.array, 0, array, 0, n); if (array.length > n) { array[n] = null; } return array; } @Override public Record subList(int fromIndex, int toIndex) { if (fromIndex < 0 || toIndex > this.itemCount || fromIndex > toIndex) { throw new IndexOutOfBoundsException(fromIndex + ", " + toIndex); } return new RecordMapView(this, fromIndex, toIndex); } private static RecordMap empty; public static RecordMap empty() { if (empty == null) { empty = new RecordMap(null, null, 0, 0, ALIASED | IMMUTABLE); } return empty; } public static RecordMap create() { return new RecordMap(null, null, 0, 0, ALIASED); } public static RecordMap create(int initialCapacity) { return new RecordMap(new Item[initialCapacity], null, 0, 0, 0); } public static RecordMap of() { return new RecordMap(null, null, 0, 0, ALIASED); } public static Record of(Object object) { final Item[] array = new Item[1]; final Item item = Item.fromObject(object); array[0] = item; return new RecordMap(array, null, 1, item instanceof Field ? 1 : 0, 0); } public static RecordMap of(Object... objects) { final int itemCount = objects.length; int fieldCount = 0; final Item[] array = new Item[itemCount]; for (int i = 0; i < itemCount; i += 1) { final Item item = Item.fromObject(objects[i]); array[i] = item; if (item instanceof Field) { fieldCount += 1; } } return new RecordMap(array, null, itemCount, fieldCount, 0); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordMapView.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.reflect.Array; import java.util.Collection; import java.util.HashSet; final class RecordMapView extends Record { RecordMap record; int lower; int upper; RecordMapView(RecordMap record, int lower, int upper) { this.record = record; this.lower = lower; this.upper = upper; } @Override public boolean isEmpty() { return this.lower == this.upper; } @Override public boolean isArray() { final Item[] array = this.record.array; for (int i = this.lower, n = this.upper; i < n; i += 1) { if (array[i] instanceof Field) { return false; } } return true; } @Override public boolean isObject() { final Item[] array = this.record.array; for (int i = this.lower, n = this.upper; i < n; i += 1) { if (array[i] instanceof Value) { return false; } } return true; } @Override public int size() { return this.upper - this.lower; } @Override public int fieldCount() { final Item[] array = this.record.array; int k = 0; for (int i = this.lower, n = this.upper; i < n; i += 1) { if (array[i] instanceof Field) { k += 1; } } return k; } @Override public int valueCount() { final Item[] array = this.record.array; int k = 0; for (int i = this.lower, n = this.upper; i < n; i += 1) { if (array[i] instanceof Value) { k += 1; } } return k; } @Override public boolean isConstant() { final Item[] array = this.record.array; for (int i = this.lower, n = this.upper; i < n; i += 1) { if (!array[i].isConstant()) { return false; } } return true; } @Override public String tag() { if (size() > 0) { final Item item = this.record.array[this.lower]; if (item instanceof Attr) { return ((Attr) item).key.value; } } return null; } @Override public Value target() { Value value = null; Record record = null; boolean modified = false; final Item[] array = this.record.array; final int n = this.upper; for (int i = this.lower; i < n; i += 1) { final Item item = array[i]; if (item instanceof Attr) { modified = true; } else if (value == null && item instanceof Value) { value = (Value) item; } else { if (record == null) { record = Record.create(); if (value != null) { record.add(value); } } record.add(item); } } if (value == null) { return Value.extant(); } else if (record == null) { return value; } else if (modified) { return record; } else { return this; } } @Override public Item head() { if (this.size() > 0) { return this.record.array[this.lower]; } else { return Item.absent(); } } @Override public Record tail() { if (size() > 0) { return new RecordMapView(this.record, this.lower + 1, this.upper); } else { return Record.empty(); } } @Override public Value body() { final int n = size(); if (n > 2) { return new RecordMapView(this.record, this.lower + 1, this.upper).branch(); } else if (n == 2) { final Item item = this.record.array[this.lower + 1]; if (item instanceof Value) { return (Value) item; } else { return Record.of(item); } } else { return Value.absent(); } } @Override public boolean contains(Item item) { final Item[] array = this.record.array; final int n = this.upper; for (int i = this.lower; i < n; i += 1) { if (array[i].equals(item)) { return true; } } return false; } @Override public boolean containsAll(Collection<?> items) { final HashSet<Object> q = new HashSet<Object>(items); final Item[] array = this.record.array; final int n = this.upper; for (int i = this.lower; i < n && !q.isEmpty(); i += 1) { q.remove(array[i]); } return q.isEmpty(); } @Override public int indexOf(Object object) { final Item item = Item.fromObject(object); final Item[] array = this.record.array; for (int i = this.lower, n = this.upper; i < n; i += 1) { if (array[i].equals(item)) { return i - this.lower; } } return -1; } @Override public int lastIndexOf(Object object) { final Item item = Item.fromObject(object); final Item[] array = this.record.array; for (int i = this.upper - 1; i >= this.lower; i -= 1) { if (array[i].equals(item)) { return i - this.lower; } } return -1; } @Override public Item get(int index) { if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException(Integer.toString(index)); } return this.record.array[this.lower + index]; } @Override public Item getItem(int index) { if (index >= 0 && index < size()) { return this.record.array[this.lower + index]; } else { return Item.absent(); } } @Override public Item setItem(int index, Item newItem) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((this.record.flags & ALIASED) != 0) { return setItemAliased(index, newItem); } else { return setItemMutable(index, newItem); } } private Item setItemAliased(int index, Item newItem) { final int n = this.record.itemCount; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(n)]; System.arraycopy(oldArray, 0, newArray, 0, n); final Item oldItem = oldArray[this.lower + index]; newArray[this.lower + index] = newItem; this.record.array = newArray; this.record.table = null; if (newItem instanceof Field) { if (!(oldItem instanceof Field)) { this.record.fieldCount += 1; } } else if (oldItem instanceof Field) { this.record.fieldCount -= 1; } FLAGS.set(this.record, this.record.flags & ~ALIASED); return oldItem; } private Item setItemMutable(int index, Item newItem) { final Item[] array = this.record.array; final Item oldItem = array[this.lower + index]; array[this.lower + index] = newItem; if (newItem instanceof Field) { this.record.table = null; if (!(oldItem instanceof Field)) { this.record.fieldCount += 1; } } else if (oldItem instanceof Field) { this.record.table = null; this.record.fieldCount -= 1; } return oldItem; } @Override public boolean add(Item newItem) { add(size(), newItem); return true; } @Override public void add(int index, Item newItem) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index > size()) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((this.record.flags & ALIASED) != 0) { addAliased(index, newItem); } else { addMutable(index, newItem); } } private void addAliased(int index, Item newItem) { final int n = this.record.itemCount; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(n + 1)]; System.arraycopy(oldArray, 0, newArray, 0, this.lower + index); System.arraycopy(oldArray, this.lower + index, newArray, this.lower + index + 1, n - (this.lower + index)); newArray[this.lower + index] = newItem; this.record.array = newArray; this.record.table = null; this.record.itemCount = n + 1; if (newItem instanceof Field) { this.record.fieldCount += 1; } FLAGS.set(this.record, this.record.flags & ~ALIASED); this.upper += 1; } private void addMutable(int index, Item newItem) { final int n = this.record.itemCount; final Item[] oldArray = this.record.array; final Item[] newArray; if (n + 1 > oldArray.length) { newArray = new Item[expand(n + 1)]; System.arraycopy(oldArray, 0, newArray, 0, this.lower + index); } else { newArray = oldArray; } System.arraycopy(oldArray, this.lower + index, newArray, this.lower + index + 1, n - (this.lower + index)); newArray[this.lower + index] = newItem; this.record.array = newArray; this.record.itemCount = n + 1; if (newItem instanceof Field) { this.record.fieldCount += 1; this.record.table = null; } this.upper += 1; } @Override public boolean addAll(Collection<? extends Item> newItems) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.record.flags & ALIASED) != 0) { return addAllAliased(size(), newItems); } else { return addAllMutable(size(), newItems); } } @Override public boolean addAll(int index, Collection<? extends Item> newItems) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index > size()) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((this.record.flags & ALIASED) != 0) { return addAllAliased(index, newItems); } else { return addAllMutable(index, newItems); } } private boolean addAllAliased(int index, Collection<? extends Item> newItems) { final int k = newItems.size(); if (k == 0) { return false; } final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(m + k)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, this.lower + index); System.arraycopy(oldArray, this.lower + index, newArray, this.lower + index + k, m - (this.lower + index)); } for (Item newItem : newItems) { newArray[this.lower + index] = newItem; index += 1; if (newItem instanceof Field) { n += 1; } } this.record.array = newArray; this.record.table = null; this.record.itemCount = m + k; this.record.fieldCount = n; FLAGS.set(this.record, this.record.flags & ~ALIASED); this.upper += k; return true; } private boolean addAllMutable(int index, Collection<? extends Item> newItems) { final int k = newItems.size(); if (k == 0) { return false; } final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] oldArray = this.record.array; final Item[] newArray; if (oldArray == null || m + k > oldArray.length) { newArray = new Item[expand(m + k)]; if (oldArray != null) { System.arraycopy(oldArray, 0, newArray, 0, this.lower + index); } } else { newArray = oldArray; } if (oldArray != null) { System.arraycopy(oldArray, this.lower + index, newArray, this.lower + index + k, m - (this.lower + index)); } for (Item newItem : newItems) { newArray[this.lower + index] = newItem; index += 1; if (newItem instanceof Field) { n += 1; this.record.table = null; } } this.record.array = newArray; this.record.itemCount = m + k; this.record.fieldCount = n; this.upper += k; return true; } @Override public Item remove(int index) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } else if (index < 0 || index >= size()) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if ((this.record.flags & ALIASED) != 0) { return removeAliased(index); } else { return removeMutable(index); } } private Item removeAliased(int index) { final int n = this.record.itemCount; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(n - 1)]; final Item oldItem = oldArray[index]; System.arraycopy(oldArray, 0, newArray, 0, this.lower + index); System.arraycopy(oldArray, this.lower + index + 1, newArray, this.lower + index, n - (this.lower + index) - 1); this.record.array = newArray; this.record.table = null; this.record.itemCount = n - 1; if (oldItem instanceof Field) { this.record.fieldCount -= 1; } FLAGS.set(this.record, this.record.flags & ~ALIASED); this.upper -= 1; return oldItem; } private Item removeMutable(int index) { final int n = this.record.itemCount; final Item[] array = this.record.array; final Item oldItem = array[this.lower + index]; System.arraycopy(array, this.lower + index + 1, array, this.lower + index, n - (this.lower + index) - 1); array[n - 1] = null; this.record.itemCount = n - 1; if (oldItem instanceof Field) { this.record.fieldCount -= 1; this.record.table = null; } this.upper -= 1; return oldItem; } @Override public boolean remove(Object object) { final Item item = Item.fromObject(object); if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } final int index = indexOf(item); if (index >= 0) { remove(index); return true; } else { return false; } } @Override public boolean removeAll(Collection<?> items) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.record.flags & ALIASED) != 0) { return removeAllAliased(items); } else { return removeAllMutable(items); } } private boolean removeAllAliased(Collection<?> items) { final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(m)]; int i = this.lower; int j = i; final int k = this.upper; System.arraycopy(oldArray, 0, newArray, 0, i); while (i < k) { final Item item = oldArray[i]; if (!items.contains(item)) { newArray[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; } i += 1; } if (i > j) { this.upper = j; while (i < m) { newArray[j] = oldArray[i]; j += 1; i += 1; } this.record.array = newArray; this.record.table = null; this.record.itemCount = j; this.record.fieldCount = n; FLAGS.set(this.record, this.record.flags & ~ALIASED); return true; } else { return false; } } private boolean removeAllMutable(Collection<?> items) { final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] array = this.record.array; int i = this.lower; int j = i; final int k = this.upper; while (i < k) { final Item item = array[i]; if (!items.contains(item)) { array[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; this.record.table = null; } i += 1; } this.upper = j; while (i < m) { array[j] = array[i]; j += 1; i += 1; } if (i > j) { while (i > j) { i -= 1; array[i] = null; } this.record.itemCount = j; this.record.fieldCount = n; return true; } else { return false; } } @Override public boolean retainAll(Collection<?> items) { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.record.flags & ALIASED) != 0) { return retainAllAliased(items); } else { return retainAllMutable(items); } } private boolean retainAllAliased(Collection<?> items) { final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(m)]; int i = this.lower; int j = i; final int k = this.upper; System.arraycopy(oldArray, 0, newArray, 0, i); while (i < k) { final Item item = oldArray[i]; if (items.contains(item)) { newArray[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; } i += 1; } if (i > j) { this.upper = j; while (i < m) { newArray[j] = oldArray[i]; j += 1; i += 1; } this.record.array = newArray; this.record.table = null; this.record.itemCount = j; this.record.fieldCount = n; FLAGS.set(this.record, this.record.flags & ~ALIASED); return true; } else { return false; } } private boolean retainAllMutable(Collection<?> items) { final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] array = this.record.array; int i = this.lower; int j = i; final int k = this.upper; while (i < k) { final Item item = array[i]; if (items.contains(item)) { array[j] = item; j += 1; } else if (item instanceof Field) { n -= 1; this.record.table = null; } i += 1; } this.upper = j; while (i < m) { array[j] = array[i]; j += 1; i += 1; } if (i > j) { while (i > j) { i -= 1; array[i] = null; } this.record.itemCount = j; this.record.fieldCount = n; return true; } else { return false; } } @Override public void clear() { if ((this.record.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } if ((this.record.flags & ALIASED) != 0) { clearAliased(); } else { clearMutable(); } } private void clearAliased() { final int m = this.record.itemCount; int n = this.record.fieldCount; final int l = m - size(); final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(l)]; System.arraycopy(oldArray, 0, newArray, 0, this.lower); int i = this.lower; while (i < n) { if (oldArray[i] instanceof Field) { n -= 1; } i += 1; } i = this.lower; int j = this.upper; while (j < m) { newArray[i] = oldArray[j]; i += 1; j += 1; } this.record.array = newArray; this.record.table = null; this.record.itemCount = l; this.record.fieldCount = n; FLAGS.set(this.record, this.record.flags & ~ALIASED); this.upper = this.lower; } private void clearMutable() { final int m = this.record.itemCount; int n = this.record.fieldCount; final Item[] array = this.record.array; int i = this.lower; while (i < n) { if (array[i] instanceof Field) { n -= 1; } i += 1; } i = this.lower; int j = this.upper; while (j < m) { final Item item = array[j]; if (item instanceof Field) { this.record.table = null; } array[i] = item; i += 1; j += 1; } this.record.itemCount = i; this.record.fieldCount = n; while (i < m) { array[i] = null; i += 1; } this.upper = this.lower; } @Override public boolean isAliased() { return (this.record.flags & ALIASED) != 0; } @Override public boolean isMutable() { return (this.record.flags & IMMUTABLE) == 0; } @Override public void alias() { FLAGS.set(this.record, this.record.flags | ALIASED); } @Override public Record branch() { final int m = size(); int n = 0; final Item[] oldArray = this.record.array; final Item[] newArray = new Item[expand(m)]; int i = this.lower; int j = 0; while (j < m) { final Item item = oldArray[i]; newArray[j] = item; if (item instanceof Field) { n += 1; } i += 1; j += 1; } return new RecordMap(newArray, null, m, n, 0); } @Override public Record commit() { this.record.commit(); return this; } @Override public Item[] toArray() { final int n = size(); final Item[] array = new Item[n]; System.arraycopy(this.record.array, this.lower, array, 0, n); return array; } @SuppressWarnings("unchecked") @Override public <T> T[] toArray(T[] array) { final int n = size(); if (array.length < n) { array = (T[]) Array.newInstance(array.getClass().getComponentType(), n); } System.arraycopy(this.record.array, this.lower, array, 0, n); if (array.length > n) { array[n] = null; } return array; } @Override public Record subList(int lower, int upper) { if (lower < 0 || upper > size() || lower > upper) { throw new IndexOutOfBoundsException(lower + ", " + upper); } return new RecordMapView(this.record, this.lower + lower, this.lower + upper); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordValueIterator.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.Iterator; final class RecordValueIterator implements Iterator<Value> { final Iterator<Item> iterator; RecordValueIterator(Iterator<Item> iterator) { this.iterator = iterator; } @Override public boolean hasNext() { return this.iterator.hasNext(); } @Override public Value next() { return this.iterator.next().toValue(); } @Override public void remove() { this.iterator.remove(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/RecordValues.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.util.AbstractCollection; import java.util.Iterator; final class RecordValues extends AbstractCollection<Value> { final Record record; RecordValues(Record record) { this.record = record; } @Override public int size() { return this.record.size(); } @Override public Iterator<Value> iterator() { return this.record.valueIterator(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Selectee.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.structure.selector.ChildrenSelector; import swim.structure.selector.KeysSelector; /** * Represents an operation that is used exclusively as a helper to some {@link * Selector Selector's} {@link Selector#mapSelected} or {@link * Selector#forSelected} methods. */ //@FunctionalInterface public interface Selectee<T> { /** * Performs this operation against {@code interpreter}. By convention, a null * return value indicates to "collection-oriented" calling {@code Selectors} * (e.g. {@link ChildrenSelector}, {@link KeysSelector}) that {@code * forSelected} should continue to be invoked, if possible. A non-null value * indicates to such {@code Selectors} that {@code forSelected} should return * immediately. */ T selected(Interpreter interpreter); }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Selector.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; import swim.structure.operator.InvokeOperator; import swim.structure.selector.ChildrenSelector; import swim.structure.selector.DescendantsSelector; import swim.structure.selector.FilterSelector; import swim.structure.selector.GetAttrSelector; import swim.structure.selector.GetItemSelector; import swim.structure.selector.GetSelector; import swim.structure.selector.IdentitySelector; import swim.structure.selector.KeysSelector; import swim.structure.selector.LiteralSelector; import swim.structure.selector.ValuesSelector; /** * An {@link Expression} that returns references to {@code Items} when it is * {@link #evaluate evaluated}. Because most application-level {@code Items} * are {@link Record Records}, a way to only extract certain parts of {@code * Records} is often required. Technically, this can be accomplished without * {@code Selectors} to some extent because the {@code Record} class implements * {@link java.util.List java.util.List&lt;Item&gt;} and (implicitly) {@link * java.util.Map java.util.Map&lt;Value,Value&gt;}; however, {@code Selectors} * additionally expose functional patterns that enhance composability, providing * a foundation on top of which expression languages can be built. */ public abstract class Selector extends Expression { @Override public boolean isConstant() { return false; } /** * Returns the {@code Selector} that this {@code Selector} uses to match * sub-selections. */ public abstract Selector then(); /** * Evaluates {@link Selectee#selected callback.selected} against the * {@code Items} that match this {@code Selector's} selection criteria. That * is, it pushes such {@code Items} to {@code interpreter}, then invokes * {@code callback} against it. To support chained {@code Selectors}, this is * a recursive procedure that invokes {@code forSelected} through * {@code this.then} wherever it exists (which it always does outside of * {@link IdentitySelector}); we define "subselection" to be such an * invocation. * * @return the result of executing {@code callback} from the context of the * last {@code Selector} in the chain formed by {@code Selector then} fields. */ public abstract <T> T forSelected(Interpreter interpreter, Selectee<T> callback); public abstract Item mapSelected(Interpreter interpreter, Selectee<Item> transform); /** * Evaluates this {@code Selector} against some {@link Interpreter}. This is * accomplished by creating a new {@link SelecteeBuilder} and populating * its internal {@link Record} with (recursive) calls to {@link #forSelected}. */ @Override public final Item evaluate(Interpreter interpreter) { final Record selected = Record.create(); final Selectee<Object> callback = new SelecteeBuilder(selected); forSelected(interpreter, callback); return selected.isEmpty() ? Item.absent() : selected.flattened(); } /** * The means to chain {@code Selectors}. By intention, this is NOT a strict * functional composition: for two {@code Selectors} {@code s1} and {@code * s2}, {@code s1.andThen(s2)} DOES NOT NECESSARILY return a new {@code * Selector} {@code s3} such that {@code s3.evaluate(args)} is equivalent to * {@code s2.evaluate(s1.evaluate(args))}. * <p> * The reason for this is that for {@code Selectors} like {@link * ChildrenSelector} that yield (logical) collections, we wish to invoke the * next {@code Selector}, say a {@link GetSelector}, against every result. * Under strict functional rules, * {@code ChildrenSelector.andThen(someGetSelector).evaluate(args)} would * instead return at most one defined value regardless of the number of * children. */ public abstract Selector andThen(Selector then); /** * An abstraction over {@link #andThen} where {@code then} is a {@link * GetSelector}. * * @param key the {@code key} field in the composing {@code GetSelector}. */ @Override public Selector get(Value key) { return andThen(new GetSelector(key, Selector.identity())); } /** * An abstraction over {@link #andThen} where {@code then} is a {@link * GetSelector}. * * @param key the {@code key} field in the composing {@code GetSelector}. */ @Override public Selector get(String key) { return get(Text.from(key)); } /** * An abstraction over {@link #andThen} where {@code then} is a {@link * GetAttrSelector}. * * @param key the {@code key} field in the composing {@code GetAttrSelector}. */ @Override public Selector getAttr(Text key) { return andThen(new GetAttrSelector(key, Selector.identity())); } /** * An abstraction over {@link #andThen} where {@code then} is a {@link * GetAttrSelector}. * * @param key the {@code key} field in the composing {@code GetAttrSelector}. */ @Override public Selector getAttr(String key) { return getAttr(Text.from(key)); } /** * An abstraction over {@link #andThen} where {@code then} is a {@link * GetItemSelector}. * * @param index the {@code index} field in the composing {@code * GetItemSelector}. */ public Selector getItem(Num index) { return andThen(new GetItemSelector(index, Selector.identity())); } /** * An abstraction over {@link #andThen} where {@code then} is a {@link * GetItemSelector}. * * @param index the {@code index} field in the composing {@code * GetItemSelector}. */ @Override public Selector getItem(int index) { return getItem(Num.from(index)); } /** * An abstraction over {@link #andThen} where {@code then} is the {@link * KeysSelector}. */ public Selector keys() { return andThen(Selector.identity().keys()); } /** * An abstraction over {@link #andThen} where {@code then} is the {@link * ValuesSelector}. */ public Selector values() { return andThen(Selector.identity().values()); } /** * An abstraction over {@link #andThen} where {@code then} is the {@link * ChildrenSelector}. */ public Selector children() { return andThen(Selector.identity().children()); } /** * An abstraction over {@link #andThen} where {@code then} is the {@link * DescendantsSelector}. */ public Selector descendants() { return andThen(Selector.identity().descendants()); } /** * Returns a new {@link FilterSelector} with {@code this} as the {@code * predicate}. */ @Override public FilterSelector filter() { return new FilterSelector(this, Selector.identity()); } /** * An abstraction over {@link #andThen} where {@code then} is a {@link * FilterSelector}. */ @Override public Selector filter(Item predicate) { return andThen(predicate.filter()); } /** * Creates, but does not evaluate, an {@link InvokeOperator} where this {@code * Selector} evaluates to the operator. */ @Override public Operator invoke(Value args) { return new InvokeOperator(this, args); } @Override public int precedence() { return 11; } @Override public void debug(Output<?> output) { output = output.write("Selector").write('.').write("identity").write('(').write(')'); debugThen(output); } public abstract void debugThen(Output<?> output); @Override public int compareTo(Item other) { if (other instanceof Selector) { return compareTo((Selector) other); } return Integer.compare(typeOrder(), other.typeOrder()); } protected abstract int compareTo(Selector that); public static Selector identity() { return IdentitySelector.identity(); } /** * Lifts {@code item} into a {@link LiteralSelector} if it is not already a * {@code Selector}. */ public static Selector literal(Item item) { if (item instanceof Selector) { return (Selector) item; } return new LiteralSelector(item, Selector.identity()); } } /** * {@link Selectee} implementation that accumulates {@link Item Items} in a * mutable {@link Record}. */ final class SelecteeBuilder implements Selectee<Object> { final Record selected; SelecteeBuilder(Record selected) { this.selected = selected; } /** * Adds the top of the {@code interpreter}'s scope stack to the {@code * selected} record. Always returns {@code null} because {@code * SelecteeBuilder} never terminates early; it accumulates all visited items * into an internal, mutable {@code Record}. */ @Override public Object selected(Interpreter interpreter) { final Item scope = interpreter.peekScope(); if (scope != null) { this.selected.add(scope); } return null; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Slot.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; import swim.structure.operator.BitwiseAndOperator; import swim.structure.operator.BitwiseOrOperator; import swim.structure.operator.BitwiseXorOperator; import swim.structure.operator.DivideOperator; import swim.structure.operator.MinusOperator; import swim.structure.operator.ModuloOperator; import swim.structure.operator.PlusOperator; import swim.structure.operator.TimesOperator; public final class Slot extends Field { final Value key; Value value; Slot(Value key, Value value, int flags) { this.key = key.commit(); this.value = value; this.flags = flags; } Slot(Value key, Value value) { this(key, value, 0); } @Override public boolean isConstant() { return this.key.isConstant() && this.value.isConstant(); } @Override public Value key() { return this.key; } @Override public Value value() { return this.value; } @Override public Value setValue(Value newValue) { if ((this.flags & IMMUTABLE) != 0) { throw new UnsupportedOperationException("immutable"); } final Value oldValue = this.value; this.value = newValue; return oldValue; } @Override public Slot updatedValue(Value value) { if (value == null) { throw new NullPointerException(); } return new Slot(key, value); } @Override public Item bitwiseOr(Item that) { if (that instanceof Expression) { return new BitwiseOrOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.bitwiseOr(((Slot) that).value); } else if (that instanceof Value) { newValue = value.bitwiseOr((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item bitwiseXor(Item that) { if (that instanceof Expression) { return new BitwiseXorOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.bitwiseXor(((Slot) that).value); } else if (that instanceof Value) { newValue = value.bitwiseXor((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item bitwiseAnd(Item that) { if (that instanceof Expression) { return new BitwiseAndOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.bitwiseAnd(((Slot) that).value); } else if (that instanceof Value) { newValue = value.bitwiseAnd((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item plus(Item that) { if (that instanceof Expression) { return new PlusOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.plus(((Slot) that).value); } else if (that instanceof Value) { newValue = value.plus((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item minus(Item that) { if (that instanceof Expression) { return new MinusOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.minus(((Slot) that).value); } else if (that instanceof Value) { newValue = value.minus((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item times(Item that) { if (that instanceof Expression) { return new TimesOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.times(((Slot) that).value); } else if (that instanceof Value) { newValue = value.times((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item divide(Item that) { if (that instanceof Expression) { return new DivideOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.divide(((Slot) that).value); } else if (that instanceof Value) { newValue = value.divide((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item modulo(Item that) { if (that instanceof Expression) { return new ModuloOperator(this, that); } final Value newValue; if (that instanceof Slot && key.equals(((Slot) that).key)) { newValue = value.modulo(((Slot) that).value); } else if (that instanceof Value) { newValue = value.modulo((Value) that); } else { newValue = Value.absent(); } if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item not() { final Value newValue = value.not(); if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item bitwiseNot() { final Value newValue = value.bitwiseNot(); if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item negative() { final Value newValue = value.negative(); if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item positive() { final Value newValue = value.positive(); if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item inverse() { final Value newValue = value.inverse(); if (newValue.isDefined()) { return new Slot(key, newValue); } return Item.absent(); } @Override public Item evaluate(Interpreter interpreter) { final Value key = this.key.evaluate(interpreter).toValue(); final Value value = this.value.evaluate(interpreter).toValue(); if (key == this.key && value == this.value) { return this; } else if (key.isDefined() && value.isDefined()) { return new Slot(key, value); } return Item.absent(); } @Override public Item substitute(Interpreter interpreter) { final Value key = this.key.substitute(interpreter).toValue(); final Value value = this.value.substitute(interpreter).toValue(); if (key == this.key && value == this.value) { return this; } else if (key.isDefined() && value.isDefined()) { return new Slot(key, value); } return Item.absent(); } @Override public boolean isAliased() { return false; } @Override public boolean isMutable() { return (this.flags & IMMUTABLE) == 0; } @Override public void alias() { do { final int oldFlags = this.flags; if ((oldFlags & IMMUTABLE) == 0) { final int newFlags = oldFlags | IMMUTABLE; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { break; } } else { break; } } while (true); } @Override public Slot branch() { if ((this.flags & IMMUTABLE) != 0) { return new Slot(key, value, flags & ~IMMUTABLE); } else { return this; } } @Override public Slot commit() { do { final int oldFlags = this.flags; if ((oldFlags & IMMUTABLE) == 0) { final int newFlags = oldFlags | IMMUTABLE; if (FLAGS.compareAndSet(this, oldFlags, newFlags)) { this.value.commit(); break; } } else { break; } } while (true); return this; } @Override public int typeOrder() { return 2; } @Override public int compareTo(Item other) { if (other instanceof Slot) { return compareTo((Slot) other); } return Integer.compare(typeOrder(), other.typeOrder()); } int compareTo(Slot that) { int order = this.key.compareTo(that.key); if (order == 0) { order = this.value.compareTo(that.value); } return order; } @Override public boolean keyEquals(Object key) { if (key instanceof String && this.key instanceof Text) { return ((Text) this.key).value.equals(key); } else if (key instanceof Field) { return this.key.equals(((Field) key).getKey()); } else { return this.key.equals(key); } } @Override public boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof Slot) { final Slot that = (Slot) other; return this.key.equals(that.key) && this.value.equals(that.value); } return false; } @Override public int hashCode() { return this.key.hashCode() ^ this.value.hashCode(); } @Override public void debug(Output<?> output) { output = output.write("Slot").write('.').write("of").write('(').display(this.key); if (!(this.value instanceof Extant)) { output = output.write(", ").display(this.value); } output = output.write(')'); } public static Slot of(Value key, Value value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Slot(key, value); } public static Slot of(Value key, String value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Slot(key, Text.from(value)); } public static Slot of(Value key, int value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(key, Num.from(value)); } public static Slot of(Value key, long value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(key, Num.from(value)); } public static Slot of(Value key, float value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(key, Num.from(value)); } public static Slot of(Value key, double value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(key, Num.from(value)); } public static Slot of(Value key, boolean value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(key, Bool.from(value)); } public static Slot of(String key, Value value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Slot(Text.from(key), value); } public static Slot of(String key, String value) { if (key == null) { throw new NullPointerException("key"); } if (value == null) { throw new NullPointerException("value"); } return new Slot(Text.from(key), Text.from(value)); } public static Slot of(String key, int value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(Text.from(key), Num.from(value)); } public static Slot of(String key, long value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(Text.from(key), Num.from(value)); } public static Slot of(String key, float value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(Text.from(key), Num.from(value)); } public static Slot of(String key, double value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(Text.from(key), Num.from(value)); } public static Slot of(String key, boolean value) { if (key == null) { throw new NullPointerException("key"); } return new Slot(Text.from(key), Bool.from(value)); } public static Slot of(Value key) { if (key == null) { throw new NullPointerException("key"); } return new Slot(key, Value.extant()); } public static Slot of(String key) { if (key == null) { throw new NullPointerException("key"); } return new Slot(Text.from(key), Value.extant()); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Tag.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Documented @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Tag { String value(); }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Text.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import swim.codec.Format; import swim.codec.Output; import swim.codec.OutputSettings; import swim.util.HashGenCacheMap; public class Text extends Value { protected final String value; protected Text(String value) { this.value = value; } @Override public boolean isConstant() { return true; } public int size() { return this.value.length(); } @Override public String stringValue() { return this.value; } @Override public String stringValue(String orElse) { return this.value; } @Override public byte byteValue() { try { return Byte.parseByte(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public byte byteValue(byte orElse) { try { return Byte.parseByte(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public short shortValue() { try { return Short.parseShort(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public short shortValue(short orElse) { try { return Short.parseShort(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public int intValue() { try { return Integer.parseInt(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public int intValue(int orElse) { try { return Integer.parseInt(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public long longValue() { try { return Long.parseLong(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public long longValue(long orElse) { try { return Long.parseLong(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public float floatValue() { try { return Float.parseFloat(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public float floatValue(float orElse) { try { return Float.parseFloat(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public double doubleValue() { try { return Double.parseDouble(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public double doubleValue(double orElse) { try { return Double.parseDouble(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public BigInteger integerValue() { try { return new BigInteger(this.value); } catch (NumberFormatException cause) { throw new UnsupportedOperationException(cause); } } @Override public BigInteger integerValue(BigInteger orElse) { try { return new BigInteger(this.value); } catch (NumberFormatException cause) { return orElse; } } @Override public Number numberValue() { return Num.from(this.value).numberValue(); } @Override public Number numberValue(Number orElse) { try { return Num.from(this.value).numberValue(); } catch (NumberFormatException cause) { return orElse; } } @Override public char charValue() { if (this.value.length() == 1) { return this.value.charAt(0); } else { throw new UnsupportedOperationException(); } } @Override public char charValue(char orElse) { try { return charValue(); } catch (UnsupportedOperationException cause) { return orElse; } } @Override public boolean booleanValue() { if ("true".equals(this.value)) { return true; } else if ("false".equals(this.value)) { return false; } else { throw new UnsupportedOperationException(); } } @Override public boolean booleanValue(boolean orElse) { if ("true".equals(this.value)) { return true; } else if ("false".equals(this.value)) { return false; } else { return orElse; } } @Override public Value plus(Value that) { if (that instanceof Text) { return plus((Text) that); } return super.plus(that); } public Text plus(Text that) { return Text.from(this.value + that.value); } @Override public Text branch() { return this; } @Override public Text commit() { return this; } @Override public int typeOrder() { return 5; } @Override public final int compareTo(Item other) { if (other instanceof Text) { return this.value.compareTo(((Text) other).value); } return Integer.compare(typeOrder(), other.typeOrder()); } @Override public final boolean equals(Object other) { if (this == other) { return true; } else if (other instanceof Text) { return this.value.equals(((Text) other).value); } return false; } @Override public final int hashCode() { // Text hashCode *must* equal String hashCode to ensure that RecordMap // hashtable lookups work with String keys. return this.value.hashCode(); } @Override public void debug(Output<?> output) { output = output.write("Text").write('.'); if (this.value.length() == 0) { output = output.write("empty").write('(').write(')'); } else { output = output.write("from").write('(').debug(this.value).write(')'); } } @Override public void display(Output<?> output) { Format.debug(this.value, output); } private static HashGenCacheMap<String, Text> cache; private static Text empty; public static Output<Text> output(OutputSettings settings) { return new TextOutput(new StringBuilder(), settings); } public static Output<Text> output() { return new TextOutput(new StringBuilder(), OutputSettings.standard()); } public static Text empty() { if (empty == null) { empty = new Text(""); } return empty; } public static Text from(String value) { final int n = value.length(); if (n == 0) { return empty(); } else if (n <= 64) { final HashGenCacheMap<String, Text> cache = cache(); Text text = cache.get(value); if (text == null) { text = cache.put(value, new Text(value)); } return text; } else { return new Text(value); } } public static Text fromObject(Object object) { if (object instanceof Text) { return (Text) object; } else if (object instanceof String) { return Text.from((String) object); } else { throw new IllegalArgumentException(object.toString()); } } static HashGenCacheMap<String, Text> cache() { if (cache == null) { int cacheSize; try { cacheSize = Integer.parseInt(System.getProperty("swim.structure.text.cache.size")); } catch (NumberFormatException e) { cacheSize = 128; } cache = new HashGenCacheMap<String, Text>(cacheSize); } return cache; } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/TextOutput.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import swim.codec.Output; import swim.codec.OutputSettings; final class TextOutput extends Output<Text> { final StringBuilder builder; OutputSettings settings; TextOutput(StringBuilder builder, OutputSettings settings) { this.builder = builder; this.settings = settings; } @Override public boolean isCont() { return true; } @Override public boolean isFull() { return false; } @Override public boolean isDone() { return false; } @Override public boolean isError() { return false; } @Override public boolean isPart() { return false; } @Override public Output<Text> isPart(boolean isPart) { return this; } @Override public Output<Text> write(int codePoint) { this.builder.appendCodePoint(codePoint); return this; } @Override public Output<Text> write(String string) { this.builder.append(string); return this; } @Override public Output<Text> writeln(String string) { this.builder.append(string).append(this.settings.lineSeparator()); return this; } @Override public Output<Text> writeln() { this.builder.append(this.settings.lineSeparator()); return this; } @Override public OutputSettings settings() { return this.settings; } @Override public Output<Text> settings(OutputSettings settings) { this.settings = settings; return this; } @Override public Text bind() { return Text.from(this.builder.toString()); } @Override public Output<Text> clone() { return new TextOutput(new StringBuilder(this.builder.toString()), this.settings); } @Override public String toString() { return this.builder.toString(); } }
0
java-sources/ai/swim/swim-structure/3.10.0/swim
java-sources/ai/swim/swim-structure/3.10.0/swim/structure/Value.java
// Copyright 2015-2019 SWIM.AI inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package swim.structure; import java.math.BigInteger; import java.nio.ByteBuffer; import swim.structure.func.LambdaFunc; import swim.structure.operator.BitwiseAndOperator; import swim.structure.operator.BitwiseOrOperator; import swim.structure.operator.BitwiseXorOperator; import swim.structure.operator.DivideOperator; import swim.structure.operator.EqOperator; import swim.structure.operator.GeOperator; import swim.structure.operator.GtOperator; import swim.structure.operator.LeOperator; import swim.structure.operator.LtOperator; import swim.structure.operator.MinusOperator; import swim.structure.operator.ModuloOperator; import swim.structure.operator.NeOperator; import swim.structure.operator.PlusOperator; import swim.structure.operator.TimesOperator; import swim.util.Builder; public abstract class Value extends Item { Value() { // stub } /** * Returns {@code true} if this {@code Value} is not {@link Absent}. */ @Override public boolean isDefined() { return true; } /** * Returns {@code true} if this {@code Value} is neither {@link Extant} nor * {@link Absent}. */ @Override public boolean isDistinct() { return true; } /** * Always returns {@link Absent} because a {@code Value} can't be a {@code * Field}, so it can't have a key component. */ @Override public final Value key() { return Value.absent(); } /** * Always returns {@code this} because every {@code Value} is its own value * component. */ @Override public final Value toValue() { return this; } /** * Returns the {@code key} string of the first member of this {@code Value}, * if this {@code Value} is a {@link Record}, and its first member is an * {@code Attr}; otherwise returns {@code null} if this {@code Value} is not * a {@code Record}, or if this {@code Value} is a {@code Record} whose first * member is not an {@code Attr}. * <p> * Used to concisely get the name of the discriminating attribute of a * structure. The {@code tag} can be used to discern the nominal type of a * polymorphic structure, similar to an XML element tag. */ @Override public String tag() { return null; } /** * Returns the {@link #flattened() flattened} members of this {@code Value} * after all attributes have been removed, if this {@code Value} is a {@link * Record}; otherwise returns {@code this} if this {@code Value} is not a * {@code Record}. * <p> * Used to concisely get the scalar value of an attributed structure. An * attributed structure is a {@code Record} with one or more attributes that * modify one or more other members. */ @Override public Value target() { return this; } /** * Returns the sole member of this {@code Value}, if this {@code Value} is a * {@link Record} with exactly one member, and its member is a {@code Value}; * returns {@link Extant} if this {@code Value} is an empty {@code Record}; * otherwise returns {@code this} if this {@code Value} is a {@code Record} * with more than one member, or if this {@code Value} is not a {@code * Record}. * <p> * Used to convert a unary {@code Record} into its member {@code Value}. * Facilitates writing code that treats a unary {@code Record} equivalently * to a bare {@code Value}. */ @Override public Value flattened() { return this; } /** * Returns {@code this} if this {@code Value} is a {@link Record}; returns a * {@code Record} containing just this {@code Value}, if this {@code Value} * is {@link #isDistinct() distinct}; otherwise returns an empty {@code * Record} if this {@code Value} is {@link Extant} or {@link Absent}. * Facilitates writing code that treats a bare {@code Value} equivalently to * a unary {@code Record}. */ @Override public Record unflattened() { return Record.of(this); } /** * Returns the value of the first member of this {@code Value}, if this * {@code Value} is a {@code Record}, and its first member is an {@link Attr} * whose {@code key} string is equal to {@code tag}; otherwise returns {@link * Absent} if this {@code Value} is not a {@code Record}, or if this {@code * Value} is a {@code Record} whose first member is not an {@code Attr}, or * if this {@code Value} is a {@code Record} whose first member is an {@code * Attr} whose {@code key} does not equal the {@code tag}. * <p> * Used to conditionally get the value of the head {@code Attr} of a * structure, if and only if the key string of the head {@code Attr} is equal * to the {@code tag}. Can be used to check if a structure might conform to * a nominal type named {@code tag}, while simultaneously getting the value * of the {@code tag} attribute. */ @Override public Value header(String tag) { return Value.absent(); } /** * Returns the {@link #unflattened() unflattened} {@link #header(String) * header} of this {@code Value}, if this {@code Value} is a {@link Record}, * and its first member is an {@link Attr} whose {@code key} string is equal * to {@code tag}; otherwise returns {@code null}. * <p> * The {@code headers} of the {@code tag} attribute of a structure are like * the attributes of an XML element tag; through unlike an XML element, * {@code tag} attribute headers are not limited to string keys and values. */ @Override public Record headers(String tag) { return null; } /** * Returns the first member of this {@code Value}, if this {@code Value} is a * non-empty {@link Record}; otherwise returns {@link Absent}. */ @Override public Item head() { return Item.absent(); } /** * Returns a view of all but the first member of this {@code Value}, if this * {@code Value} is a non-empty {@link Record}; otherwise returns an empty * {@code Record} if this {@code Value} is not a {@code Record}, of if this * {@code Value} is itself an empty {@code Record}. */ @Override public Record tail() { return Record.empty(); } /** * Returns the {@link Record#flattened() flattened} {@link #tail() tail} * of this {@code Value}. Used to recursively deconstruct a structure, * terminating with its last {@code Value}, rather than a unary {@code * Record} containing its last value, if the structure ends with a {@code * Value} member. */ @Override public Value body() { return Value.extant(); } /** * Returns the number of members contained in this {@code Value}, if this * {@code Value} is a {@link Record}; otherwise returns {@code 0} if this * {@code Value} is not a {@code Record}. */ @Override public int length() { return 0; } /** * Returns {@code true} if this {@code Value} is a {@link Record} that has a * member equal to {@code item}; otherwise returns {@code false} if this * {@code Value} is not a {@code Record}, or if this {@code Value} is a * {@code Record}, but has no member equal to {@code item}. */ @Override public boolean contains(Item item) { return false; } /** * Returns {@code true} if this {@code Value} is a {@link Record} that has a * {@link Field} member with a key that is equal to the given {@code key}; * otherwise returns {@code false} if this {@code Value} is not a {@code * Record}, or if this {@code Value} is a {@code Record}, but has no {@code * Field} member with a key equal to the given {@code key}. */ @Override public boolean containsKey(Value key) { return false; } /** * Returns {@code true} if this {@code Value} is a {@link Record} that has a * {@link Field} with a {@code Text} key whose string value is equal to the * given {@code key}; otherwise returns {@code false} if this {@code Value} * is not a {@code Record}, or if this {@code Value} is a {@code Record}, but * has no {@code Field} member with a {@code Text} key whose string value * equals the given {@code key}. Equivalent to {@link #containsKey(Value)}, * but avoids boxing the {@code key} string into a {@code Text} value. */ @Override public boolean containsKey(String key) { return false; } /** * Returns {@code true} if this {@code Value} is a {@link Record} that has a * {@link Field} member with a value that is equal to the given {@code value}; * otherwise returns {@code false} if this {@code Value} is not a {@code * Record}, or if this {@code Value} is a {@code Record}, but has no {@code * Field} member with a value equal to the given {@code value}. */ @Override public boolean containsValue(Value value) { return false; } /** * Returns the value of the last {@link Field} member of this {@code Value} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Value} is not a {@link Record}, or if this {@code Value} is a * {@code Record}, but has no {@code Field} member with a key equal to the * given {@code key}. */ @Override public Value get(Value key) { return Value.absent(); } /** * Returns the value of the last {@link Field} member of this {@code Value} * with a {@code Text} key whose string value is equal to the given {@code * key}; returns {@link Absent} if this {@code Value} is not a {@link Record}, * or if this {@code Value} is a {@code Record}, but has no {@code Field} * member with a {@code Text} key whose string value equals the given {@code * key}. Equivalent to {@link #get(Value)}, but avoids boxing the {@code * key} string into a {@code Text} value. */ @Override public Value get(String key) { return Value.absent(); } /** * Returns the value of the last {@link Attr} member of this {@code Value} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Value} is not a {@link Record}, or if this {@code Value} is a * {@code Record}, but has no {@code Attr} member with a key equal to the * given {@code key}. */ @Override public Value getAttr(Text key) { return Value.absent(); } /** * Returns the value of the last {@link Attr} member of this {@code Value} * with a {@code Text} key whose string value is equal to the given {@code * key}; returns {@link Absent} if this {@code Value} is not a {@link Record}, * or if this {@code Value} is a {@code Record}, but has no {@code Attr} * member with a {@code Text} key whose string value equals the given {@code * key}. Equivalent to {@link #getAttr(Text)}, but avoids boxing the {@code * key} string into a {@code Text} value. */ @Override public Value getAttr(String key) { return Value.absent(); } /** * Returns the value of the last {@link Slot} member of this {@code Value} * whose key is equal to the given {@code key}; returns {@link Absent} if * this {@code Value} is not a {@link Record}, or if this {@code Value} is a * {@code Record}, but has no {@code Slot} member with a key equal to the * given {@code key}. */ @Override public Value getSlot(Value key) { return Value.absent(); } /** * Returns the value of the last {@link Slot} member of this {@code Value} * with a {@code Text} key whose string value is equal to the given {@code * key}; returns {@link Absent} if this {@code Value} is not a {@link Record}, * or if this {@code Value} is a {@code Record}, but has no {@code Slot} * member with a {@code Text} key whose string value equals the given {@code * key}. Equivalent to {@link #getSlot(Value)}, but avoids boxing the {@code * key} string into a {@code Text} value. */ @Override public Value getSlot(String key) { return Value.absent(); } /** * Returns the last {@link Field} member of this {@code Value} whose key is * equal to the given {@code key}; returns {@code null} if this {@code Value} * is not a {@link Record}, or if this {@code Value} is a {@code Record}, but * has no {@code Field} member with a {@code key} equal to the given * {@code key}. */ @Override public Field getField(Value key) { return null; } /** * Returns the last {@link Field} member of this {@code Value} with a {@code * Text} key whose string value is equal to the given {@code key}; returns * {@code null} if this {@code Value} is not a {@link Record}, or if this * {@code Value} is a {@code Record}, but has no {@code Field} member with a * {@code Text} key whose string value equals the given {@code key}. * Equivalent to {@link #getField(Value)}, but avoids boxing the {@code key} * string into a {@code Text} value. */ @Override public Field getField(String key) { return null; } /** * Returns the member of this {@code Value} at the given {@code index}, if * this {@code Value} is a {@link Record}, and the {@code index} is greater * than or equal to zero, and less than the {@link Record#length() length} of * the {@code Record}; otherwise returns {@link Absent} if this {@code Value} * is not a {@code Record}, or if this {@code Value} is a {@code Record}, but * the {@code index} is out of bounds. */ @Override public Item getItem(int index) { return Item.absent(); } @Override public Value removed(Value key) { return this; } @Override public Value removed(String key) { return this; } @Override public Item conditional(Item thenTerm, Item elseTerm) { if (thenTerm instanceof Value && elseTerm instanceof Value) { return conditional((Value) thenTerm, (Value) elseTerm); } return thenTerm; } public Value conditional(Value thenTerm, Value elseTerm) { return thenTerm; } @Override public Item or(Item that) { if (that instanceof Value) { return or((Value) that); } return this; } public Value or(Value that) { return this; } @Override public Item and(Item that) { if (that instanceof Value) { return and((Value) that); } return that; } public Value and(Value that) { return that; } @Override public Item bitwiseOr(Item that) { if (that instanceof Value) { return bitwiseOr((Value) that); } else if (that instanceof Slot) { final Value newValue = bitwiseOr(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = bitwiseOr(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value bitwiseOr(Value that) { if (that instanceof Expression) { return new BitwiseOrOperator(this, that); } return Value.absent(); } @Override public Item bitwiseXor(Item that) { if (that instanceof Value) { return bitwiseXor((Value) that); } else if (that instanceof Slot) { final Value newValue = bitwiseXor(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = bitwiseXor(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value bitwiseXor(Value that) { if (that instanceof Expression) { return new BitwiseXorOperator(this, that); } return Value.absent(); } @Override public Item bitwiseAnd(Item that) { if (that instanceof Value) { return bitwiseAnd((Value) that); } else if (that instanceof Slot) { final Value newValue = bitwiseAnd(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = bitwiseAnd(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value bitwiseAnd(Value that) { if (that instanceof Expression) { return new BitwiseAndOperator(this, that); } return Value.absent(); } @Override public Item lt(Item that) { if (that instanceof Value) { return lt((Value) that); } return super.lt(that); } public Value lt(Value that) { if (that instanceof Expression) { return new LtOperator(this, that); } return (Value) super.lt(that); } @Override public Item le(Item that) { if (that instanceof Value) { return le((Value) that); } return super.le(that); } public Value le(Value that) { if (that instanceof Expression) { return new LeOperator(this, that); } return (Value) super.le(that); } @Override public Item eq(Item that) { if (that instanceof Value) { return eq((Value) that); } return super.eq(that); } public Value eq(Value that) { if (that instanceof Expression) { return new EqOperator(this, that); } return (Value) super.eq(that); } @Override public Item ne(Item that) { if (that instanceof Value) { return ne((Value) that); } return super.ne(that); } public Value ne(Value that) { if (that instanceof Expression) { return new NeOperator(this, that); } return (Value) super.ne(that); } @Override public Item ge(Item that) { if (that instanceof Value) { return ge((Value) that); } return super.ge(that); } public Value ge(Value that) { if (that instanceof Expression) { return new GeOperator(this, that); } return (Value) super.ge(that); } @Override public Item gt(Item that) { if (that instanceof Value) { return gt((Value) that); } return super.gt(that); } public Value gt(Value that) { if (that instanceof Expression) { return new GtOperator(this, that); } return (Value) super.gt(that); } @Override public Item plus(Item that) { if (that instanceof Value) { return plus((Value) that); } else if (that instanceof Slot) { final Value newValue = plus(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = plus(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value plus(Value that) { if (that instanceof Expression) { return new PlusOperator(this, that); } return Value.absent(); } @Override public Item minus(Item that) { if (that instanceof Value) { return minus((Value) that); } else if (that instanceof Slot) { final Value newValue = minus(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = minus(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value minus(Value that) { if (that instanceof Expression) { return new MinusOperator(this, that); } return Value.absent(); } @Override public Item times(Item that) { if (that instanceof Value) { return times((Value) that); } else if (that instanceof Slot) { final Value newValue = times(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = times(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value times(Value that) { if (that instanceof Expression) { return new TimesOperator(this, that); } return Value.absent(); } @Override public Item divide(Item that) { if (that instanceof Value) { return divide((Value) that); } else if (that instanceof Slot) { final Value newValue = divide(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = divide(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value divide(Value that) { if (that instanceof Expression) { return new DivideOperator(this, that); } return Value.absent(); } @Override public Item modulo(Item that) { if (that instanceof Value) { return modulo((Value) that); } else if (that instanceof Slot) { final Value newValue = modulo(((Slot) that).value); if (newValue.isDefined()) { return new Slot(((Slot) that).key, newValue); } } else if (that instanceof Attr) { final Value newValue = modulo(((Attr) that).value); if (newValue.isDefined()) { return new Attr(((Attr) that).key, newValue); } } return Item.absent(); } public Value modulo(Value that) { if (that instanceof Expression) { return new ModuloOperator(this, that); } return Value.absent(); } @Override public Value not() { return Value.absent(); } @Override public Value bitwiseNot() { return Value.absent(); } @Override public Value negative() { return Value.absent(); } @Override public Value positive() { return Value.absent(); } @Override public Value inverse() { return Value.absent(); } @Override public Value lambda(Value template) { return new LambdaFunc(this, template); } /** * Converts this {@code Value} into a {@code String} value, if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a {@code String} value. */ @Override public String stringValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a {@code String} value, if possible; * otherwise returns {@code orElse} if this {@code Value} can't be converted * into a {@code string} value. */ @Override public String stringValue(String orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code byte} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code byte} value. */ @Override public byte byteValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code byte} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code byte} value. */ @Override public byte byteValue(byte orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code short} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code short} value. */ @Override public short shortValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code short} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code short} value. */ @Override public short shortValue(short orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code int} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code int} value. */ @Override public int intValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code int} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code int} value. */ @Override public int intValue(int orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code long} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code long} value. */ @Override public long longValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code long} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code long} value. */ @Override public long longValue(long orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code float} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code float} value. */ @Override public float floatValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code float} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code float} value. */ @Override public float floatValue(float orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code double} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code double} value. */ @Override public double doubleValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code double} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code double} value. */ @Override public double doubleValue(double orElse) { return orElse; } /** * Converts this {@code Value} into a {@code BigInteger} value, if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a {@code BigInteger} value. */ @Override public BigInteger integerValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a {@code BigInteger} value, if possible; * otherwise returns {@code orElse} if this {@code Value} can't be converted * into a {@code BigInteger} value. */ @Override public BigInteger integerValue(BigInteger orElse) { return orElse; } /** * Converts this {@code Value} into a {@code Number} object, if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a {@code Number} object. */ @Override public Number numberValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a {@code Number} object, if possible; * otherwise returns {@code orElse} if this {@code Value} can't be converted * into a {@code Number} object. */ @Override public Number numberValue(Number orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code char} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code char} value. */ @Override public char charValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code char} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code char} value. */ @Override public char charValue(char orElse) { return orElse; } /** * Converts this {@code Value} into a primitive {@code boolean} value, * if possible. * * @throws UnsupportedOperationException if this {@code Value} can't be * converted into a primitive {@code boolean} value. */ @Override public boolean booleanValue() { throw new UnsupportedOperationException(); } /** * Converts this {@code Value} into a primitive {@code boolean} value, * if possible; otherwise returns {@code orElse} if this {@code Value} can't * be converted into a primitive {@code boolean} value. */ @Override public boolean booleanValue(boolean orElse) { return orElse; } @Override public boolean isAliased() { return false; } @Override public boolean isMutable() { return false; } @Override public void alias() { // nop } @Override public Value branch() { return this; } @Override public Value commit() { return this; } @Override public boolean keyEquals(Object key) { return false; } public static Builder<Item, Value> builder() { return new ValueBuilder(); } public static Value empty() { return Record.empty(); } public static Value extant() { return Extant.extant(); } public static Value absent() { return Absent.absent(); } public static Value fromObject(Object object) { if (object == null) { return Value.extant(); } else if (object instanceof Value) { return (Value) object; } else if (object instanceof String) { return Text.from((String) object); } else if (object instanceof Number) { return Num.from((Number) object); } else if (object instanceof Character) { return Num.from(((Character) object).charValue()); } else if (object instanceof Boolean) { return Bool.from(((Boolean) object).booleanValue()); } else if (object instanceof ByteBuffer) { return Data.from((ByteBuffer) object); } else if (object instanceof byte[]) { return Data.wrap((byte[]) object); } else { throw new IllegalArgumentException(object.toString()); } } }