index int64 | repo_id string | file_path string | content string |
|---|---|---|---|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/ValueBuilder.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.Collection;
import swim.util.Builder;
final class ValueBuilder implements Builder<Item, Value> {
Record record = null;
Value value = null;
@Override
public boolean add(Item item) {
if (item instanceof Field) {
return addField((Field) item);
} else if (item instanceof Value) {
return addValue((Value) item);
} else {
throw new AssertionError(item);
}
}
@Override
public boolean addAll(Collection<? extends Item> items) {
for (Item item : items) {
add(item);
}
return true;
}
boolean addField(Field item) {
if (this.record == null) {
this.record = Record.create();
if (this.value != null) {
this.record.add(this.value);
this.value = null;
}
}
this.record.add(item);
return true;
}
boolean addValue(Value item) {
if (this.record != null) {
this.record.add(item);
} else if (this.value == null) {
this.value = item;
} else {
this.record = Record.create();
this.record.add(this.value);
this.value = null;
this.record.add(item);
}
return true;
}
@Override
public Value bind() {
if (this.record != null) {
return this.record;
} else if (this.value != null) {
return this.value;
} else {
return Value.absent();
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/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.
/**
* Uniform structured data model.
*/
package swim.structure;
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueCollection.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.collections;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import swim.structure.Form;
import swim.structure.Record;
import swim.structure.Value;
public class ValueCollection<T> extends ValueIterable<T> implements Collection<T> {
public ValueCollection(Collection<? extends Value> inner, Form<T> valueForm) {
super(inner, valueForm);
}
public Collection<Value> inner() {
return (Collection<Value>) this.inner;
}
@Override
public <T2> ValueCollection<T2> valueForm(Form<T2> valueForm) {
return new ValueCollection<T2>((Collection<Value>) this.inner, valueForm);
}
@Override
public <T2> ValueCollection<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@Override
public boolean isEmpty() {
return ((Collection<Value>) this.inner).isEmpty();
}
@Override
public int size() {
return ((Collection<Value>) this.inner).size();
}
@SuppressWarnings("unchecked")
@Override
public boolean contains(Object object) {
final Class<?> valueType = this.valueForm.type();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
return ((Collection<Value>) this.inner).contains(value);
}
return false;
}
@Override
public boolean containsAll(Collection<?> objects) {
final Iterator<?> those = objects.iterator();
while (those.hasNext()) {
if (!contains(those.next())) {
return false;
}
}
return true;
}
@Override
public boolean add(T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
return ((Collection<Value>) this.inner).add(newValue);
}
@SuppressWarnings("unchecked")
@Override
public boolean addAll(Collection<? extends T> newObjects) {
if (this.valueForm != Form.forValue()) {
final Iterator<?> those = newObjects.iterator();
final Class<?> valueType = this.valueForm.type();
final Record values = Record.create(newObjects.size());
while (those.hasNext()) {
final Object newObject = those.next();
if (valueType == null || valueType.isInstance(newObject)) {
final Value newValue = this.valueForm.mold((T) newObject).toValue();
values.add(newValue);
}
}
return ((Collection<Value>) this.inner).addAll((Collection<Value>) (Collection<?>) values);
} else {
return ((Collection<Value>) this.inner).addAll((Collection<? extends Value>) newObjects);
}
}
@SuppressWarnings("unchecked")
@Override
public boolean remove(Object object) {
final Class<?> valueType = this.valueForm.type();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
return ((Collection<Value>) this.inner).remove(value);
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public boolean removeAll(Collection<?> objects) {
if (this.valueForm != Form.forValue()) {
final Class<?> valueType = this.valueForm.type();
final Iterator<?> those = objects.iterator();
boolean modified = false;
while (those.hasNext()) {
final Object object = those.next();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
if (((Collection<Value>) this.inner).remove(value)) {
modified = true;
}
}
}
return modified;
} else {
return ((Collection<Value>) this.inner).removeAll((Collection<Value>) objects);
}
}
@SuppressWarnings("unchecked")
@Override
public boolean retainAll(Collection<?> objects) {
if (this.valueForm != Form.forValue()) {
final Iterator<?> those = objects.iterator();
final Class<?> valueType = this.valueForm.type();
final Record values = Record.create(objects.size());
while (those.hasNext()) {
final Object object = those.next();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
values.add(value);
}
}
return ((Collection<Value>) this.inner).retainAll(values);
} else {
return ((Collection<Value>) this.inner).retainAll((Collection<Value>) objects);
}
}
@Override
public void clear() {
((Collection<Value>) this.inner).clear();
}
@Override
public Object[] toArray() {
final Iterator<Value> these = ((Collection<Value>) this.inner).iterator();
int i = 0;
int n = ((Collection<Value>) this.inner).size();
Object[] array = new Object[n];
while (these.hasNext()) {
final Value value = these.next();
T object = this.valueForm.cast(value);
if (object == null) {
object = this.valueForm.unit();
}
if (i == n) {
n = n + (n >> 1) + 1;
if (n < 0) {
n = Integer.MAX_VALUE;
}
final Object[] newArray = new Object[n];
System.arraycopy(array, 0, newArray, 0, i);
array = newArray;
}
array[i] = object;
i += 1;
}
if (i < n) {
final Object[] newArray = new Object[i];
System.arraycopy(array, 0, newArray, 0, i);
array = newArray;
}
return array;
}
@SuppressWarnings("unchecked")
@Override
public <T2> T2[] toArray(T2[] a) {
final Iterator<Value> these = ((Collection<Value>) this.inner).iterator();
int i = 0;
int n = a.length;
T2[] array = a;
while (these.hasNext()) {
final Value value = these.next();
T object = this.valueForm.cast(value);
if (object == null) {
object = this.valueForm.unit();
}
if (i == n) {
n = n + (n >> 1) + 1;
if (n < 0) {
n = Integer.MAX_VALUE;
}
final T2[] newArray = (T2[]) Array.newInstance(a.getClass().getComponentType(), n);
System.arraycopy(array, 0, newArray, 0, i);
array = newArray;
}
array[i] = (T2) object;
i += 1;
}
if (i < n) {
if (array == a) {
array[i] = null;
} else {
final T2[] newArray = (T2[]) Array.newInstance(a.getClass().getComponentType(), i);
System.arraycopy(array, 0, newArray, 0, i);
array = newArray;
}
}
return array;
}
@Override
public String toString() {
final Iterator<T> these = iterator();
if (!these.hasNext()) {
return "[]";
}
final StringBuilder sb = new StringBuilder();
sb.append('[');
do {
sb.append(these.next());
if (these.hasNext()) {
sb.append(", ");
} else {
break;
}
} while (true);
return sb.append(']').toString();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueCombinerFunction.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.collections;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.CombinerFunction;
final class ValueCombinerFunction<V, U> implements CombinerFunction<Value, Value> {
final CombinerFunction<? super V, U> combiner;
final Form<U> resultForm;
final Form<V> elementForm;
ValueCombinerFunction(CombinerFunction<? super V, U> combiner, Form<U> resultForm, Form<V> elementForm) {
this.combiner = combiner;
this.resultForm = resultForm;
this.elementForm = elementForm;
}
@Override
public Value combine(Value resultValue, Value elementValue) {
U result = this.resultForm.cast(resultValue);
final V element = this.elementForm.cast(elementValue);
result = this.combiner.combine(result, element);
resultValue = this.resultForm.mold(result).toValue();
return resultValue;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueCursor.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.collections;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.Cursor;
public class ValueCursor<T> implements Cursor<T> {
protected Cursor<Value> inner;
protected Form<T> valueForm;
@SuppressWarnings("unchecked")
public ValueCursor(Cursor<? extends Value> inner, Form<T> valueForm) {
this.inner = (Cursor<Value>) inner;
this.valueForm = valueForm;
}
public Cursor<Value> inner() {
return this.inner;
}
public Form<T> valueForm() {
return this.valueForm;
}
public <T2> ValueCursor<T2> valueForm(Form<T2> valueForm) {
return new ValueCursor<T2>(this.inner, valueForm);
}
public <T2> ValueCursor<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@Override
public boolean isEmpty() {
return this.inner.isEmpty();
}
@Override
public T head() {
final Value value = this.inner.head();
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public void step() {
this.inner.step();
}
@Override
public void skip(long count) {
this.inner.skip(count);
}
@Override
public boolean hasNext() {
return this.inner.hasNext();
}
@Override
public long nextIndexLong() {
return this.inner.nextIndexLong();
}
@Override
public int nextIndex() {
return this.inner.nextIndex();
}
@Override
public T next() {
final Value value = this.inner.next();
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public boolean hasPrevious() {
return this.inner.hasPrevious();
}
@Override
public long previousIndexLong() {
return this.inner.previousIndexLong();
}
@Override
public int previousIndex() {
return this.inner.previousIndex();
}
@Override
public T previous() {
final Value value = this.inner.previous();
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public void set(T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
this.inner.set(newValue);
}
@Override
public void add(T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
this.inner.add(newValue);
}
@Override
public void remove() {
this.inner.remove();
}
public void load() throws InterruptedException {
this.inner.load();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueEntry.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.collections;
import java.util.Map;
import swim.structure.Form;
import swim.structure.Value;
public final class ValueEntry<K, V> implements Map.Entry<K, V> {
final Map.Entry<Value, Value> inner;
final Form<K> keyForm;
final Form<V> valueForm;
volatile K keyObject;
volatile V valueObject;
public ValueEntry(Map.Entry<Value, Value> inner, Form<K> keyForm, Form<V> valueForm) {
if (inner == null) {
throw new NullPointerException();
}
this.inner = inner;
this.keyForm = keyForm;
this.valueForm = valueForm;
}
@Override
public K getKey() {
K keyObject = this.keyObject;
if (keyObject == null) {
keyObject = this.keyForm.cast(this.inner.getKey());
if (keyObject == null) {
keyObject = this.keyForm.unit();
}
this.keyObject = keyObject;
}
return keyObject;
}
@Override
public V getValue() {
V valueObject = this.valueObject;
if (valueObject == null) {
valueObject = this.valueForm.cast(this.inner.getValue());
if (valueObject == null) {
valueObject = this.valueForm.unit();
}
this.valueObject = valueObject;
}
return valueObject;
}
@Override
public V setValue(V newValueObject) {
final Value newValue = this.valueForm.mold(newValueObject).toValue();
final Value oldValue = this.inner.setValue(newValue);
final V oldValueObject = this.valueForm.cast(oldValue);
if (oldValueObject != null) {
return oldValueObject;
}
return this.valueForm.unit();
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Map.Entry<?, ?>) {
final Map.Entry<?, ?> that = (Map.Entry<?, ?>) other;
final K keyObject = getKey();
if (keyObject == null ? that.getKey() != null : !keyObject.equals(that.getKey())) {
return false;
}
final V valueObject = getValue();
if (valueObject == null ? that.getValue() != null : !valueObject.equals(that.getValue())) {
return false;
}
return true;
}
return false;
}
@Override
public int hashCode() {
final K keyObject = getKey();
final V valueObject = getValue();
return (keyObject == null ? 0 : keyObject.hashCode())
^ (valueObject == null ? 0 : valueObject.hashCode());
}
@Override
public String toString() {
return new StringBuilder().append(getKey()).append('=').append(getValue()).toString();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueEntryCursor.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.collections;
import java.util.Map;
import swim.structure.Form;
import swim.structure.Slot;
import swim.structure.Value;
import swim.util.Cursor;
public class ValueEntryCursor<K, V> implements Cursor<Map.Entry<K, V>> {
final Cursor<Map.Entry<Value, Value>> inner;
final Form<K> keyForm;
final Form<V> valueForm;
@SuppressWarnings("unchecked")
public ValueEntryCursor(Cursor<? extends Map.Entry<? extends Value, ? extends Value>> inner, Form<K> keyForm, Form<V> valueForm) {
this.inner = (Cursor<Map.Entry<Value, Value>>) inner;
this.keyForm = keyForm;
this.valueForm = valueForm;
}
public Cursor<Map.Entry<Value, Value>> inner() {
return this.inner;
}
public Form<K> keyForm() {
return this.keyForm;
}
public <K2> ValueEntryCursor<K2, V> keyForm(Form<K2> keyForm) {
return new ValueEntryCursor<K2, V>(this.inner, keyForm, this.valueForm);
}
public <K2> ValueEntryCursor<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
public Form<V> valueForm() {
return this.valueForm;
}
public <V2> ValueEntryCursor<K, V2> valueForm(Form<V2> valueForm) {
return new ValueEntryCursor<K, V2>(this.inner, this.keyForm, valueForm);
}
public <V2> ValueEntryCursor<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@Override
public boolean isEmpty() {
return this.inner.isEmpty();
}
@Override
public Map.Entry<K, V> head() {
return new ValueEntry<K, V>(this.inner.head(), this.keyForm, this.valueForm);
}
@Override
public void step() {
this.inner.step();
}
@Override
public void skip(long count) {
this.inner.skip(count);
}
@Override
public boolean hasNext() {
return this.inner.hasNext();
}
@Override
public long nextIndexLong() {
return this.inner.nextIndexLong();
}
@Override
public int nextIndex() {
return this.inner.nextIndex();
}
@Override
public Map.Entry<K, V> next() {
return new ValueEntry<K, V>(this.inner.next(), this.keyForm, this.valueForm);
}
@Override
public boolean hasPrevious() {
return this.inner.hasPrevious();
}
@Override
public long previousIndexLong() {
return this.inner.previousIndexLong();
}
@Override
public int previousIndex() {
return this.inner.previousIndex();
}
@Override
public Map.Entry<K, V> previous() {
return new ValueEntry<K, V>(this.inner.previous(), this.keyForm, this.valueForm);
}
@Override
public void set(Map.Entry<K, V> newEntry) {
final Value newKey = this.keyForm.mold(newEntry.getKey()).toValue();
final Value newValue = this.valueForm.mold(newEntry.getValue()).toValue();
this.inner.set(Slot.of(newKey, newValue));
}
@Override
public void add(Map.Entry<K, V> newEntry) {
final Value newKey = this.keyForm.mold(newEntry.getKey()).toValue();
final Value newValue = this.valueForm.mold(newEntry.getValue()).toValue();
this.inner.add(Slot.of(newKey, newValue));
}
@Override
public void remove() {
this.inner.remove();
}
public void load() throws InterruptedException {
this.inner.load();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueEntryIterator.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.collections;
import java.util.Iterator;
import java.util.Map;
import swim.structure.Form;
import swim.structure.Value;
public final class ValueEntryIterator<K, V> implements Iterator<Map.Entry<K, V>> {
final Iterator<Map.Entry<Value, Value>> inner;
final Form<K> keyForm;
final Form<V> valueForm;
@SuppressWarnings("unchecked")
public ValueEntryIterator(Iterator<? extends Map.Entry<? extends Value, ? extends Value>> inner, Form<K> keyForm, Form<V> valueForm) {
this.inner = (Iterator<Map.Entry<Value, Value>>) inner;
this.keyForm = keyForm;
this.valueForm = valueForm;
}
public Iterator<Map.Entry<Value, Value>> inner() {
return this.inner;
}
public Form<K> keyForm() {
return this.keyForm;
}
public <K2> ValueEntryIterator<K2, V> keyForm(Form<K2> keyForm) {
return new ValueEntryIterator<K2, V>(this.inner, keyForm, this.valueForm);
}
public <K2> ValueEntryIterator<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
public Form<V> valueForm() {
return this.valueForm;
}
public <V2> ValueEntryIterator<K, V2> valueForm(Form<V2> valueForm) {
return new ValueEntryIterator<K, V2>(this.inner, this.keyForm, valueForm);
}
public <V2> ValueEntryIterator<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@Override
public boolean hasNext() {
return this.inner.hasNext();
}
@Override
public Map.Entry<K, V> next() {
return new ValueEntry<K, V>(this.inner.next(), this.keyForm, this.valueForm);
}
@Override
public void remove() {
this.inner.remove();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueIterable.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.collections;
import java.util.Iterator;
import swim.structure.Form;
import swim.structure.Value;
public class ValueIterable<T> implements Iterable<T> {
protected Iterable<Value> inner;
protected Form<T> valueForm;
@SuppressWarnings("unchecked")
public ValueIterable(Iterable<? extends Value> inner, Form<T> valueForm) {
this.inner = (Iterable<Value>) inner;
this.valueForm = valueForm;
}
public Iterable<Value> inner() {
return this.inner;
}
public Form<T> valueForm() {
return this.valueForm;
}
public <T2> ValueIterable<T2> valueForm(Form<T2> valueForm) {
return new ValueIterable<T2>(this.inner, valueForm);
}
public <T2> ValueIterable<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@SuppressWarnings("unchecked")
@Override
public Iterator<T> iterator() {
if (this.valueForm == Form.forValue()) {
return (Iterator<T>) this.inner.iterator();
}
return new ValueIterator<T>(this.inner.iterator(), this.valueForm);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueIterableMap.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.collections;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.Cursor;
import swim.util.IterableMap;
public class ValueIterableMap<K, V> extends ValueMap<K, V> implements IterableMap<K, V> {
public ValueIterableMap(IterableMap<? extends Value, ? extends Value> inner, Form<K> keyForm, Form<V> valueForm) {
super(inner, keyForm, valueForm);
}
public IterableMap<Value, Value> inner() {
return (IterableMap<Value, Value>) this.inner;
}
public <K2> ValueIterableMap<K2, V> keyForm(Form<K2> keyForm) {
return new ValueIterableMap<K2, V>((IterableMap<Value, Value>) this.inner, keyForm, this.valueForm);
}
public <K2> ValueIterableMap<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
public <V2> ValueIterableMap<K, V2> valueForm(Form<V2> valueForm) {
return new ValueIterableMap<K, V2>((IterableMap<Value, Value>) this.inner, this.keyForm, valueForm);
}
public <V2> ValueIterableMap<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@SuppressWarnings("unchecked")
@Override
public Cursor<Entry<K, V>> iterator() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new ValueEntryCursor<K, V>(((IterableMap<Value, Value>) this.inner).iterator(), this.keyForm, this.valueForm);
} else {
return (Cursor<Entry<K, V>>) (Cursor<?>) ((IterableMap<Value, Value>) this.inner).iterator();
}
}
@SuppressWarnings("unchecked")
public Cursor<K> keyIterator() {
if (this.keyForm != Form.forValue()) {
return new ValueCursor<K>(((IterableMap<Value, Value>) this.inner).keyIterator(), this.keyForm);
} else {
return (Cursor<K>) ((IterableMap<Value, Value>) this.inner).keyIterator();
}
}
@SuppressWarnings("unchecked")
public Cursor<V> valueIterator() {
if (this.valueForm != Form.forValue()) {
return new ValueCursor<V>(((IterableMap<Value, Value>) this.inner).valueIterator(), this.valueForm);
} else {
return (Cursor<V>) ((IterableMap<Value, Value>) this.inner).valueIterator();
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueIterator.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.collections;
import java.util.Iterator;
import swim.structure.Form;
import swim.structure.Value;
public class ValueIterator<T> implements Iterator<T> {
protected Iterator<Value> inner;
protected Form<T> valueForm;
@SuppressWarnings("unchecked")
public ValueIterator(Iterator<? extends Value> inner, Form<T> valueForm) {
this.inner = (Iterator<Value>) inner;
this.valueForm = valueForm;
}
public Iterator<Value> inner() {
return this.inner;
}
public Form<T> valueForm() {
return this.valueForm;
}
public <T2> ValueIterator<T2> valueForm(Form<T2> valueForm) {
return new ValueIterator<T2>(this.inner, valueForm);
}
public <T2> ValueIterator<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@Override
public boolean hasNext() {
return this.inner.hasNext();
}
@Override
public T next() {
final Value value = this.inner.next();
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public void remove() {
this.inner.remove();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueKeyedList.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.collections;
import java.util.AbstractMap;
import java.util.ListIterator;
import java.util.Map;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.KeyedList;
public class ValueKeyedList<T> extends ValueList<T> implements KeyedList<T> {
public ValueKeyedList(KeyedList<? extends Value> inner, Form<T> valueForm) {
super(inner, valueForm);
}
@Override
public KeyedList<Value> inner() {
return (KeyedList<Value>) this.inner;
}
@Override
public <T2> ValueKeyedList<T2> valueForm(Form<T2> valueForm) {
return new ValueKeyedList<T2>((KeyedList<Value>) this.inner, valueForm);
}
@Override
public <T2> ValueKeyedList<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@Override
public T get(int index, Object key) {
final Value value = ((KeyedList<Value>) this.inner).get(index, key);
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public Map.Entry<Object, T> getEntry(int index) {
final Map.Entry<Object, Value> entry = ((KeyedList<Value>) this.inner).getEntry(index);
T object = this.valueForm.cast(entry.getValue());
if (object == null) {
object = this.valueForm.unit();
}
return new AbstractMap.SimpleImmutableEntry<Object, T>(entry.getKey(), object);
}
@Override
public Map.Entry<Object, T> getEntry(int index, Object key) {
final Map.Entry<Object, Value> entry = ((KeyedList<Value>) this.inner).getEntry(index, key);
if (entry != null) {
T object = this.valueForm.cast(entry.getValue());
if (object == null) {
object = this.valueForm.unit();
}
return new AbstractMap.SimpleImmutableEntry<Object, T>(entry.getKey(), object);
}
return null;
}
@Override
public T set(int index, T newObject, Object key) {
final Value newValue = this.valueForm.mold(newObject).toValue();
final Value oldValue = ((KeyedList<Value>) this.inner).set(index, newValue, key);
final T oldObject = this.valueForm.cast(oldValue);
if (oldObject != null) {
return oldObject;
}
return this.valueForm.unit();
}
@Override
public boolean add(T newObject, Object key) {
final Value newValue = this.valueForm.mold(newObject).toValue();
return ((KeyedList<Value>) this.inner).add(newValue, key);
}
@Override
public void add(int index, T newObject, Object key) {
final Value newValue = this.valueForm.mold(newObject).toValue();
((KeyedList<Value>) this.inner).add(index, newValue, key);
}
@Override
public T remove(int index, Object key) {
final Value value = ((KeyedList<Value>) this.inner).remove(index, key);
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public void move(int fromIndex, int toIndex) {
((KeyedList<Value>) this.inner).move(fromIndex, toIndex);
}
@Override
public void move(int fromIndex, int toIndex, Object key) {
((KeyedList<Value>) this.inner).move(fromIndex, toIndex, key);
}
@SuppressWarnings("unchecked")
@Override
public ListIterator<Object> keyIterator() {
return ((KeyedList<Value>) this.inner).keyIterator();
}
@SuppressWarnings("unchecked")
@Override
public ListIterator<Map.Entry<Object, T>> entryIterator() {
if (this.valueForm != Form.forValue()) {
return new ValueKeyedListEntryIterator<T>(((KeyedList<Value>) this.inner).entryIterator(), this.valueForm);
} else {
return (ListIterator<Map.Entry<Object, T>>) (ListIterator<?>) ((KeyedList<Value>) this.inner).entryIterator();
}
}
}
final class ValueKeyedListEntryIterator<T> implements ListIterator<Map.Entry<Object, T>> {
final ListIterator<Map.Entry<Object, Value>> inner;
final Form<T> valueForm;
ValueKeyedListEntryIterator(ListIterator<Map.Entry<Object, Value>> inner, Form<T> valueForm) {
this.inner = inner;
this.valueForm = valueForm;
}
@Override
public boolean hasNext() {
return this.inner.hasNext();
}
@Override
public int nextIndex() {
return this.inner.nextIndex();
}
@Override
public Map.Entry<Object, T> next() {
final Map.Entry<Object, Value> next = this.inner.next();
final Object nextKey = next.getKey();
final T nextObject = this.valueForm.cast(next.getValue());
return new AbstractMap.SimpleImmutableEntry<Object, T>(nextKey, nextObject);
}
@Override
public boolean hasPrevious() {
return this.inner.hasPrevious();
}
@Override
public int previousIndex() {
return this.inner.previousIndex();
}
@Override
public Map.Entry<Object, T> previous() {
final Map.Entry<Object, Value> previous = this.inner.previous();
final Object previousKey = previous.getKey();
final T previousObject = this.valueForm.cast(previous.getValue());
return new AbstractMap.SimpleImmutableEntry<Object, T>(previousKey, previousObject);
}
@Override
public void add(Map.Entry<Object, T> newEntry) {
throw new UnsupportedOperationException();
}
@Override
public void set(Map.Entry<Object, T> newEntry) {
throw new UnsupportedOperationException();
}
@Override
public void remove() {
this.inner.remove();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueList.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.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import swim.structure.Form;
import swim.structure.Record;
import swim.structure.Value;
public class ValueList<T> extends ValueCollection<T> implements List<T> {
public ValueList(List<? extends Value> inner, Form<T> valueForm) {
super(inner, valueForm);
}
@Override
public List<Value> inner() {
return (List<Value>) this.inner;
}
@Override
public <T2> ValueList<T2> valueForm(Form<T2> valueForm) {
return new ValueList<T2>((List<Value>) this.inner, valueForm);
}
@Override
public <T2> ValueList<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@SuppressWarnings("unchecked")
@Override
public int indexOf(Object object) {
final Class<?> valueType = this.valueForm.type();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
return ((List<Value>) this.inner).indexOf(value);
}
return -1;
}
@SuppressWarnings("unchecked")
@Override
public int lastIndexOf(Object object) {
final Class<?> valueType = this.valueForm.type();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
return ((List<Value>) this.inner).lastIndexOf(value);
}
return -1;
}
@Override
public T get(int index) {
final Value value = ((List<Value>) this.inner).get(index);
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public T set(int index, T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
final Value oldValue = ((List<Value>) this.inner).set(index, newValue);
final T oldObject = this.valueForm.cast(oldValue);
if (oldObject != null) {
return oldObject;
}
return this.valueForm.unit();
}
@Override
public void add(int index, T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
((List<Value>) this.inner).add(index, newValue);
}
@SuppressWarnings("unchecked")
@Override
public boolean addAll(int index, Collection<? extends T> newObjects) {
final Iterator<?> those = newObjects.iterator();
final Class<?> valueType = this.valueForm.type();
final Record values = Record.create(newObjects.size());
while (those.hasNext()) {
final Object object = those.next();
if (valueType == null || valueType.isInstance(object)) {
final Value value = this.valueForm.mold((T) object).toValue();
values.add(value);
}
}
return ((List<Value>) this.inner).addAll(index, (Collection<Value>) (Collection<?>) values);
}
@Override
public T remove(int index) {
final Value value = ((List<Value>) this.inner).remove(index);
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@SuppressWarnings("unchecked")
@Override
public List<T> subList(int fromIndex, int toIndex) {
if (this.valueForm != Form.forValue()) {
return new ValueList<T>(((List<Value>) this.inner).subList(fromIndex, toIndex), this.valueForm);
} else {
return (List<T>) ((List<Value>) this.inner).subList(fromIndex, toIndex);
}
}
@SuppressWarnings("unchecked")
@Override
public ListIterator<T> listIterator(int index) {
if (this.valueForm != Form.forValue()) {
return new ValueListIterator<T>(((List<Value>) this.inner).listIterator(index), this.valueForm);
} else {
return (ListIterator<T>) ((List<Value>) this.inner).listIterator();
}
}
@SuppressWarnings("unchecked")
@Override
public ListIterator<T> listIterator() {
if (this.valueForm != Form.forValue()) {
return new ValueListIterator<T>(((List<Value>) this.inner).listIterator(), this.valueForm);
} else {
return (ListIterator<T>) ((List<Value>) this.inner).listIterator();
}
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof List<?>) {
final Iterator<T> these = iterator();
final Iterator<?> those = ((List<?>) other).iterator();
while (these.hasNext() && those.hasNext()) {
final T x = these.next();
final Object y = those.next();
if (!(x == null ? y == null : x.equals(y))) {
return false;
}
}
return !(these.hasNext() || those.hasNext());
} else {
return false;
}
}
@Override
public int hashCode() {
final Iterator<T> these = iterator();
int code = 0;
while (these.hasNext()) {
final T object = these.next();
code = 31 * code + (object == null ? 0 : object.hashCode());
}
return code;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueListIterator.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.collections;
import java.util.ListIterator;
import swim.structure.Form;
import swim.structure.Value;
public class ValueListIterator<T> extends ValueIterator<T> implements ListIterator<T> {
public ValueListIterator(ListIterator<? extends Value> inner, Form<T> valueForm) {
super(inner, valueForm);
}
@Override
public <T2> ValueListIterator<T2> valueForm(Form<T2> valueForm) {
return new ValueListIterator<T2>((ListIterator<Value>) this.inner, valueForm);
}
@Override
public <T2> ValueListIterator<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@Override
public ListIterator<Value> inner() {
return (ListIterator<Value>) this.inner;
}
@Override
public int nextIndex() {
return ((ListIterator<Value>) this.inner).nextIndex();
}
@Override
public boolean hasPrevious() {
return ((ListIterator<Value>) this.inner).hasPrevious();
}
@Override
public int previousIndex() {
return ((ListIterator<Value>) this.inner).previousIndex();
}
@Override
public T previous() {
final Value value = ((ListIterator<Value>) this.inner).previous();
final T object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public void add(T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
((ListIterator<Value>) this.inner).add(newValue);
}
@Override
public void set(T newObject) {
final Value newValue = this.valueForm.mold(newObject).toValue();
((ListIterator<Value>) this.inner).set(newValue);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueMap.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.collections;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import swim.structure.Form;
import swim.structure.Value;
public class ValueMap<K, V> implements Map<K, V> {
protected Map<Value, Value> inner;
protected Form<K> keyForm;
protected Form<V> valueForm;
@SuppressWarnings("unchecked")
public ValueMap(Map<? extends Value, ? extends Value> inner, Form<K> keyForm, Form<V> valueForm) {
this.inner = (Map<Value, Value>) inner;
this.keyForm = keyForm;
this.valueForm = valueForm;
}
public Map<Value, Value> inner() {
return this.inner;
}
public Form<K> keyForm() {
return this.keyForm;
}
public <K2> ValueMap<K2, V> keyForm(Form<K2> keyForm) {
return new ValueMap<K2, V>(this.inner, keyForm, this.valueForm);
}
public <K2> ValueMap<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
public Form<V> valueForm() {
return this.valueForm;
}
public <V2> ValueMap<K, V2> valueForm(Form<V2> valueForm) {
return new ValueMap<K, V2>(this.inner, this.keyForm, valueForm);
}
public <V2> ValueMap<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@Override
public boolean isEmpty() {
return this.inner.isEmpty();
}
@Override
public int size() {
return this.inner.size();
}
@SuppressWarnings("unchecked")
@Override
public boolean containsKey(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
return this.inner.containsKey(key);
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public boolean containsValue(Object valueObject) {
final Class<?> valueType = this.valueForm.type();
if (valueType == null || valueType.isInstance(valueObject)) {
final Value value = this.valueForm.mold((V) valueObject).toValue();
return this.inner.containsValue(value);
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public V get(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value value = this.inner.get(key);
final V valueObject = this.valueForm.cast(value);
if (valueObject != null) {
return valueObject;
}
}
return this.valueForm.unit();
}
@Override
public V put(K keyObject, V newValueObject) {
final Value key = this.keyForm.mold(keyObject).toValue();
final Value newValue = this.valueForm.mold(newValueObject).toValue();
final Value oldValue = this.inner.put(key, newValue);
final V oldValueObject = this.valueForm.cast(oldValue);
if (oldValueObject != null) {
return oldValueObject;
}
return this.valueForm.unit();
}
@SuppressWarnings("unchecked")
@Override
public void putAll(Map<? extends K, ? extends V> map) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
} else {
this.inner.putAll((Map<? extends Value, ? extends Value>) map);
}
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value oldValue = this.inner.remove(key);
final V oldValueObject = this.valueForm.cast(oldValue);
if (oldValueObject != null) {
return oldValueObject;
}
}
return this.valueForm.unit();
}
@Override
public void clear() {
this.inner.clear();
}
@SuppressWarnings("unchecked")
@Override
public Set<Entry<K, V>> entrySet() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new ValueMapEntrySet<K, V>(this.inner, this.keyForm, this.valueForm);
} else {
return (Set<Entry<K, V>>) (Set<?>) this.inner.entrySet();
}
}
@SuppressWarnings("unchecked")
@Override
public Set<K> keySet() {
if (this.keyForm != Form.forValue()) {
return new ValueSet<K>(this.inner.keySet(), this.keyForm);
} else {
return (Set<K>) this.inner.keySet();
}
}
@SuppressWarnings("unchecked")
@Override
public Collection<V> values() {
if (this.valueForm != Form.forValue()) {
return new ValueCollection<V>(this.inner.values(), this.valueForm);
} else {
return (Collection<V>) this.inner.values();
}
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Map<?, ?>) {
final Map<?, ?> that = (Map<?, ?>) other;
if (size() != that.size()) {
return false;
}
try {
final Iterator<Entry<K, V>> these = entrySet().iterator();
while (these.hasNext()) {
final Entry<K, V> entry = these.next();
final K keyObject = entry.getKey();
final V valueObject = entry.getValue();
if (valueObject != null) {
if (!valueObject.equals(that.get(keyObject))) {
return false;
}
} else {
if (!(that.get(keyObject) == null && that.containsKey(keyObject))) {
return false;
}
}
}
return true;
} catch (ClassCastException | NullPointerException e) {
// swallow
}
}
return false;
}
@Override
public int hashCode() {
final Iterator<Entry<K, V>> these = entrySet().iterator();
int code = 0;
while (these.hasNext()) {
code += these.next().hashCode();
}
return code;
}
@Override
public String toString() {
final Iterator<Entry<K, V>> these = entrySet().iterator();
final StringBuilder sb = new StringBuilder();
sb.append('{');
if (these.hasNext()) {
sb.append(these.next());
while (these.hasNext()) {
sb.append(", ").append(these.next());
}
}
sb.append('}');
return sb.toString();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueMapEntrySet.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.collections;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import swim.structure.Form;
import swim.structure.Value;
public class ValueMapEntrySet<K, V> extends AbstractSet<Map.Entry<K, V>> {
protected Map<Value, Value> inner;
protected Form<K> keyForm;
protected Form<V> valueForm;
@SuppressWarnings("unchecked")
public ValueMapEntrySet(Map<? extends Value, ? extends Value> inner, Form<K> keyForm, Form<V> valueForm) {
this.inner = (Map<Value, Value>) inner;
this.keyForm = keyForm;
this.valueForm = valueForm;
}
@Override
public int size() {
return this.inner.size();
}
@Override
public Iterator<Map.Entry<K, V>> iterator() {
return new ValueEntryIterator<K, V>(this.inner.entrySet().iterator(), this.keyForm, this.valueForm);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueOrderedMap.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.collections;
import java.util.Comparator;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.IterableMap;
import swim.util.OrderedMap;
import swim.util.OrderedMapCursor;
public class ValueOrderedMap<K, V> extends ValueIterableMap<K, V> implements OrderedMap<K, V> {
public ValueOrderedMap(OrderedMap<? extends Value, ? extends Value> inner, Form<K> keyForm, Form<V> valueForm) {
super((IterableMap<? extends Value, ? extends Value>) inner, keyForm, valueForm);
}
@Override
public OrderedMap<Value, Value> inner() {
return (OrderedMap<Value, Value>) this.inner;
}
@Override
public <K2> ValueOrderedMap<K2, V> keyForm(Form<K2> keyForm) {
return new ValueOrderedMap<K2, V>((OrderedMap<Value, Value>) this.inner, keyForm, this.valueForm);
}
@Override
public <K2> ValueOrderedMap<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
@Override
public <V2> ValueOrderedMap<K, V2> valueForm(Form<V2> valueForm) {
return new ValueOrderedMap<K, V2>((OrderedMap<Value, Value>) this.inner, this.keyForm, valueForm);
}
@Override
public <V2> ValueOrderedMap<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@SuppressWarnings("unchecked")
@Override
public int indexOf(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
return ((OrderedMap<Value, Value>) this.inner).indexOf(key);
}
throw new IllegalArgumentException(keyObject.toString());
}
@SuppressWarnings("unchecked")
@Override
public Entry<K, V> getEntry(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Entry<Value, Value> entry = ((OrderedMap<Value, Value>) this.inner).getEntry(key);
if (entry != null) {
return new ValueEntry<K, V>(entry, this.keyForm, this.valueForm);
}
}
return null;
}
@Override
public Entry<K, V> getIndex(int index) {
final Entry<Value, Value> entry = ((OrderedMap<Value, Value>) this.inner).getIndex(index);
if (entry != null) {
return new ValueEntry<K, V>(entry, this.keyForm, this.valueForm);
}
return null;
}
@Override
public Entry<K, V> firstEntry() {
final Entry<Value, Value> entry = ((OrderedMap<Value, Value>) this.inner).firstEntry();
if (entry != null) {
return new ValueEntry<K, V>(entry, this.keyForm, this.valueForm);
}
return null;
}
@Override
public K firstKey() {
final Value key = ((OrderedMap<Value, Value>) this.inner).firstKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@Override
public V firstValue() {
final Value value = ((OrderedMap<Value, Value>) this.inner).firstValue();
final V object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public Entry<K, V> lastEntry() {
final Entry<Value, Value> entry = ((OrderedMap<Value, Value>) this.inner).lastEntry();
if (entry != null) {
return new ValueEntry<K, V>(entry, this.keyForm, this.valueForm);
}
return null;
}
@Override
public K lastKey() {
final Value key = ((OrderedMap<Value, Value>) this.inner).lastKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@Override
public V lastValue() {
final Value value = ((OrderedMap<Value, Value>) this.inner).lastValue();
final V object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@SuppressWarnings("unchecked")
@Override
public Entry<K, V> nextEntry(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Entry<Value, Value> entry = ((OrderedMap<Value, Value>) this.inner).nextEntry(key);
if (entry != null) {
return new ValueEntry<K, V>(entry, this.keyForm, this.valueForm);
}
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public K nextKey(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value nextKey = ((OrderedMap<Value, Value>) this.inner).nextKey(key);
final K nextKeyObject = this.keyForm.cast(nextKey);
if (nextKeyObject != null) {
return nextKeyObject;
}
return this.keyForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public V nextValue(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value nextValue = ((OrderedMap<Value, Value>) this.inner).nextValue(key);
final V nextObject = this.valueForm.cast(nextValue);
if (nextObject != null) {
return nextObject;
}
return this.valueForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public Entry<K, V> previousEntry(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Entry<Value, Value> entry = ((OrderedMap<Value, Value>) this.inner).previousEntry(key);
if (entry != null) {
return new ValueEntry<K, V>(entry, this.keyForm, this.valueForm);
}
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public K previousKey(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value previousKey = ((OrderedMap<Value, Value>) this.inner).previousKey(key);
final K previousKeyObject = this.keyForm.cast(previousKey);
if (previousKeyObject != null) {
return previousKeyObject;
}
return this.keyForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public V previousValue(Object keyObject) {
final Class<?> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value previousValue = ((OrderedMap<Value, Value>) this.inner).previousValue(key);
final V previousObject = this.valueForm.cast(previousValue);
if (previousObject != null) {
return previousObject;
}
return this.valueForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public OrderedMap<K, V> headMap(K toKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value toKey = this.keyForm.mold(toKeyObject).toValue();
return new ValueOrderedMap<K, V>(((OrderedMap<Value, Value>) this.inner).headMap(toKey), this.keyForm, this.valueForm);
} else {
return (OrderedMap<K, V>) (OrderedMap<?, ?>) ((OrderedMap<Value, Value>) this.inner).headMap((Value) toKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public OrderedMap<K, V> tailMap(K fromKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value fromKey = this.keyForm.mold(fromKeyObject).toValue();
return new ValueOrderedMap<K, V>(((OrderedMap<Value, Value>) this.inner).tailMap(fromKey), this.keyForm, this.valueForm);
} else {
return (OrderedMap<K, V>) (OrderedMap<?, ?>) ((OrderedMap<Value, Value>) this.inner).tailMap((Value) fromKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public OrderedMap<K, V> subMap(K fromKeyObject, K toKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value fromKey = this.keyForm.mold(fromKeyObject).toValue();
final Value toKey = this.keyForm.mold(toKeyObject).toValue();
return new ValueOrderedMap<K, V>(((OrderedMap<Value, Value>) this.inner).subMap(fromKey, toKey), this.keyForm, this.valueForm);
} else {
return (OrderedMap<K, V>) (OrderedMap<?, ?>) ((OrderedMap<Value, Value>) this.inner).subMap((Value) fromKeyObject, (Value) toKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public OrderedMapCursor<K, V> iterator() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new ValueOrderedMapCursor<K, V>(((OrderedMap<Value, Value>) this.inner).iterator(), this.keyForm, this.valueForm);
} else {
return (OrderedMapCursor<K, V>) (OrderedMapCursor<?, ?>) ((OrderedMap<Value, Value>) this.inner).iterator();
}
}
@Override
public Comparator<? super K> comparator() {
return null;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueOrderedMapCursor.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.collections;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.OrderedMapCursor;
public class ValueOrderedMapCursor<K, V> extends ValueEntryCursor<K, V> implements OrderedMapCursor<K, V> {
public ValueOrderedMapCursor(OrderedMapCursor<? extends Value, ? extends Value> inner, Form<K> keyForm, Form<V> valueForm) {
super(inner, keyForm, valueForm);
}
@Override
public OrderedMapCursor<Value, Value> inner() {
return (OrderedMapCursor<Value, Value>) this.inner;
}
@Override
public <K2> ValueOrderedMapCursor<K2, V> keyForm(Form<K2> keyForm) {
return new ValueOrderedMapCursor<K2, V>((OrderedMapCursor<Value, Value>) this.inner, keyForm, this.valueForm);
}
@Override
public <K2> ValueOrderedMapCursor<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
@Override
public <V2> ValueOrderedMapCursor<K, V2> valueForm(Form<V2> valueForm) {
return new ValueOrderedMapCursor<K, V2>((OrderedMapCursor<Value, Value>) this.inner, this.keyForm, valueForm);
}
@Override
public <V2> ValueOrderedMapCursor<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@Override
public K nextKey() {
final Value key = ((OrderedMapCursor<Value, Value>) this.inner).nextKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@Override
public K previousKey() {
final Value key = ((OrderedMapCursor<Value, Value>) this.inner).previousKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueReducedMap.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.collections;
import swim.structure.Form;
import swim.structure.Value;
import swim.util.CombinerFunction;
import swim.util.OrderedMap;
import swim.util.ReducedMap;
public class ValueReducedMap<K, V, U> extends ValueOrderedMap<K, V> implements ReducedMap<K, V, U> {
protected Form<U> reducedForm;
public ValueReducedMap(ReducedMap<? extends Value, ? extends Value, ? extends Value> inner,
Form<K> keyForm, Form<V> valueForm, Form<U> reducedForm) {
super((OrderedMap<? extends Value, ? extends Value>) inner, keyForm, valueForm);
this.reducedForm = reducedForm;
}
@SuppressWarnings("unchecked")
@Override
public ReducedMap<Value, Value, Value> inner() {
return (ReducedMap<Value, Value, Value>) this.inner;
}
@SuppressWarnings("unchecked")
@Override
public <K2> ValueReducedMap<K2, V, U> keyForm(Form<K2> keyForm) {
return new ValueReducedMap<K2, V, U>((ReducedMap<Value, Value, Value>) this.inner,
keyForm, this.valueForm, this.reducedForm);
}
@Override
public <K2> ValueReducedMap<K2, V, U> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
@SuppressWarnings("unchecked")
@Override
public <V2> ValueReducedMap<K, V2, U> valueForm(Form<V2> valueForm) {
return new ValueReducedMap<K, V2, U>((ReducedMap<Value, Value, Value>) this.inner,
this.keyForm, valueForm, this.reducedForm);
}
@Override
public <V2> ValueReducedMap<K, V2, U> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
public Form<U> reducedForm() {
return this.reducedForm;
}
@SuppressWarnings("unchecked")
public <U2> ValueReducedMap<K, V, U2> reducedForm(Form<U2> reducedForm) {
return new ValueReducedMap<K, V, U2>((ReducedMap<Value, Value, Value>) this.inner,
this.keyForm, this.valueForm, reducedForm);
}
public <U2> ValueReducedMap<K, V, U2> reducedClass(Class<U2> reducedClass) {
return reducedForm(Form.<U2>forClass(reducedClass));
}
@SuppressWarnings("unchecked")
@Override
public U reduced(U identity, CombinerFunction<? super V, U> accumulator, CombinerFunction<U, U> combiner) {
final CombinerFunction<Value, Value> valueAccumulator = new ValueCombinerFunction<V, U>(accumulator, this.reducedForm, this.valueForm);
final CombinerFunction<Value, Value> valueCombiner = new ValueCombinerFunction<U, U>(combiner, this.reducedForm, this.reducedForm);
final Value identityValue = this.reducedForm.mold(identity).toValue();
final Value reducedValue = ((ValueReducedMap<Value, Value, Value>) this.inner).reduced(identityValue, valueAccumulator, valueCombiner);
final U reduced = this.reducedForm.cast(reducedValue);
return reduced;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueSet.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.collections;
import java.util.Iterator;
import java.util.Set;
import swim.structure.Form;
import swim.structure.Value;
public class ValueSet<T> extends ValueCollection<T> implements Set<T> {
public ValueSet(Set<? extends Value> inner, Form<T> valueForm) {
super(inner, valueForm);
}
@Override
public Set<Value> inner() {
return (Set<Value>) this.inner;
}
@Override
public <T2> ValueSet<T2> valueForm(Form<T2> valueForm) {
return new ValueSet<T2>((Set<Value>) this.inner, valueForm);
}
@Override
public <T2> ValueSet<T2> valueClass(Class<T2> valueClass) {
return valueForm(Form.<T2>forClass(valueClass));
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Set<?>) {
final Set<?> that = (Set<?>) other;
if (size() == that.size()) {
try {
return containsAll(that);
} catch (ClassCastException | NullPointerException e) {
// swallow
}
}
}
return false;
}
@Override
public int hashCode() {
final Iterator<T> these = iterator();
int code = 0;
while (these.hasNext()) {
final T object = these.next();
if (object != null) {
code += object.hashCode();
}
}
return code;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/ValueSortedMap.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.collections;
import java.util.Comparator;
import java.util.SortedMap;
import swim.structure.Form;
import swim.structure.Value;
public class ValueSortedMap<K, V> extends ValueMap<K, V> implements SortedMap<K, V> {
public ValueSortedMap(SortedMap<Value, Value> inner, Form<K> keyForm, Form<V> valueForm) {
super(inner, keyForm, valueForm);
}
@Override
public SortedMap<Value, Value> inner() {
return (SortedMap<Value, Value>) this.inner;
}
@Override
public <K2> ValueSortedMap<K2, V> keyForm(Form<K2> keyForm) {
return new ValueSortedMap<K2, V>((SortedMap<Value, Value>) this.inner, keyForm, this.valueForm);
}
@Override
public <K2> ValueSortedMap<K2, V> keyClass(Class<K2> keyClass) {
return keyForm(Form.<K2>forClass(keyClass));
}
@Override
public <V2> ValueSortedMap<K, V2> valueForm(Form<V2> valueForm) {
return new ValueSortedMap<K, V2>((SortedMap<Value, Value>) this.inner, this.keyForm, valueForm);
}
@Override
public <V2> ValueSortedMap<K, V2> valueClass(Class<V2> valueClass) {
return valueForm(Form.<V2>forClass(valueClass));
}
@Override
public K firstKey() {
final Value key = ((SortedMap<Value, Value>) this.inner).firstKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@Override
public K lastKey() {
final Value key = ((SortedMap<Value, Value>) this.inner).lastKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@SuppressWarnings("unchecked")
@Override
public SortedMap<K, V> headMap(K toKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value toKey = this.keyForm.mold(toKeyObject).toValue();
return new ValueSortedMap<K, V>(((SortedMap<Value, Value>) this.inner).headMap(toKey), this.keyForm, this.valueForm);
} else {
return (SortedMap<K, V>) (SortedMap<?, ?>) ((SortedMap<Value, Value>) this.inner).headMap((Value) toKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public SortedMap<K, V> tailMap(K fromKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value fromKey = this.keyForm.mold(fromKeyObject).toValue();
return new ValueSortedMap<K, V>(((SortedMap<Value, Value>) this.inner).tailMap(fromKey), this.keyForm, this.valueForm);
} else {
return (SortedMap<K, V>) (SortedMap<?, ?>) ((SortedMap<Value, Value>) this.inner).tailMap((Value) fromKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public SortedMap<K, V> subMap(K fromKeyObject, K toKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value fromKey = this.keyForm.mold(fromKeyObject).toValue();
final Value toKey = this.keyForm.mold(toKeyObject).toValue();
return new ValueSortedMap<K, V>(((SortedMap<Value, Value>) this.inner).subMap(fromKey, toKey), this.keyForm, this.valueForm);
} else {
return (SortedMap<K, V>) (SortedMap<?, ?>) ((SortedMap<Value, Value>) this.inner).subMap((Value) fromKeyObject, (Value) toKeyObject);
}
}
@Override
public Comparator<? super K> comparator() {
return null;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/collections/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.
/**
* Nominally typed views of structurally typed collections.
*/
package swim.structure.collections;
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ArrayForm.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.form;
import java.lang.reflect.Array;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
/**
* For some type, a transformation between a structurally typed {@code Item} and
* an array of elements with that type.
*/
public final class ArrayForm extends Form<Object> {
final Class<?> type;
final Form<Object> form;
Object unit;
/**
* @param type the {@code Class} to which <i>elements</i> of the desired
* arrays belong
* @param form the desired {@code Form} that transforms instances of {@code type}
*/
@SuppressWarnings("unchecked")
public ArrayForm(Class<?> type, Form<?> form) {
this.type = type;
this.form = (Form<Object>) form;
}
@Override
public Object unit() {
if (this.unit == null) {
this.unit = Array.newInstance(this.type, 0);
}
return this.unit;
}
@Override
public Class<?> type() {
return unit().getClass();
}
@Override
public Item mold(Object array, Item item) {
if (array != null) {
final int n = Array.getLength(array);
for (int i = 0; i < n; i += 1) {
item = item.appended(this.form.mold(Array.get(array, i)));
}
return item;
} else {
return Item.extant();
}
}
@Override
public Item mold(Object array) {
if (array != null) {
final int n = Array.getLength(array);
final Record record = Record.create(n);
for (int i = 0; i < n; i += 1) {
record.add(this.form.mold(Array.get(array, i)));
}
return record;
} else {
return Item.extant();
}
}
@Override
public Object cast(Item item) {
final Value value = item.toValue();
final int n = value.length();
if (n > 0) {
final Object array = Array.newInstance(this.type, n);
int k = 0;
for (Item child : value) {
final Object elem = this.form.cast(child);
if (elem != null) {
Array.set(array, k, elem);
k += 1;
}
}
if (k == n) {
return array;
} else {
final Object newArray = Array.newInstance(this.type, k);
System.arraycopy(array, 0, newArray, 0, k);
return newArray;
}
} else if (value.isDefined()) {
final Object elem = this.form.cast(value);
if (elem != null) {
final Object array = Array.newInstance(this.type, 1);
Array.set(array, 0, elem);
return array;
}
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/AttrForm.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.form;
import swim.structure.Attr;
import swim.structure.Field;
import swim.structure.Form;
import swim.structure.FormException;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Text;
import swim.structure.Value;
public final class AttrForm<T> extends FieldForm<T> {
final java.lang.reflect.Field field;
final Text key;
final Form<?> form;
public AttrForm(java.lang.reflect.Field field, Text key, Form<?> form) {
this.field = field;
this.key = key;
this.form = form;
}
@Override
public java.lang.reflect.Field field() {
return this.field;
}
@Override
public Text key() {
return this.key;
}
@Override
public Class<?> type() {
return this.field.getDeclaringClass();
}
@SuppressWarnings("unchecked")
@Override
public Item mold(T object, Item item) {
if (object != null) {
try {
final Value value = ((Form<Object>) this.form).mold(this.field.get(object)).toValue();
if (item instanceof Field) {
return ((Field) item).updatedValue(value);
} else {
return item.updatedAttr(this.key, value);
}
} catch (IllegalAccessException cause) {
return Item.absent();
}
} else {
return Item.extant();
}
}
@SuppressWarnings("unchecked")
@Override
public Item mold(T object) {
if (object != null) {
try {
final Value value = ((Form<Object>) this.form).mold(this.field.get(object)).toValue();
return Attr.of(this.key, value);
} catch (IllegalAccessException cause) {
return Item.absent();
}
} else {
return Item.extant();
}
}
@Override
public T cast(Item item, T object) {
try {
if (item instanceof Record) {
final Value value = item.get(this.key);
if (value.isDefined()) {
this.field.set(object, this.form.cast(value));
}
} else if (item.keyEquals(this.key)) {
this.field.set(object, this.form.cast(item.toValue()));
}
} catch (IllegalAccessException cause) {
throw new FormException(cause);
}
return object;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/BigIntegerForm.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.form;
import java.math.BigInteger;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* BigInteger}.
*/
public final class BigIntegerForm extends Form<BigInteger> {
final BigInteger unit;
public BigIntegerForm(BigInteger unit) {
this.unit = unit;
}
@Override
public BigInteger unit() {
return this.unit;
}
@Override
public Form<BigInteger> unit(BigInteger unit) {
return new BigIntegerForm(unit);
}
@Override
public Class<BigInteger> type() {
return BigInteger.class;
}
@Override
public Item mold(BigInteger value) {
if (value != null) {
return Num.from(value);
} else {
return Item.extant();
}
}
@Override
public BigInteger cast(Item item) {
final Value value = item.target();
try {
return value.integerValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/BooleanForm.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.form;
import swim.structure.Bool;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* Double}.
*/
public final class BooleanForm extends Form<Boolean> {
final Boolean unit;
public BooleanForm(Boolean unit) {
this.unit = unit;
}
@Override
public Boolean unit() {
return this.unit;
}
@Override
public Form<Boolean> unit(Boolean unit) {
return new BooleanForm(unit);
}
@Override
public Class<Boolean> type() {
return Boolean.class;
}
@Override
public Item mold(Boolean value) {
if (value != null) {
return Bool.from(value.booleanValue());
} else {
return Item.extant();
}
}
@Override
public Boolean cast(Item item) {
final Value value = item.target();
try {
return value.booleanValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ByteBufferForm.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.form;
import java.nio.ByteBuffer;
import swim.structure.Data;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* ByteBuffer}.
*/
public final class ByteBufferForm extends Form<ByteBuffer> {
@Override
public Class<ByteBuffer> type() {
return ByteBuffer.class;
}
@Override
public Item mold(ByteBuffer value) {
if (value != null) {
return Data.from(value);
} else {
return Item.extant();
}
}
@Override
public ByteBuffer cast(Item item) {
final Value value = item.target();
if (value instanceof Data) {
return ((Data) value).toByteBuffer();
} else {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ByteForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link Byte}.
*/
public final class ByteForm extends Form<Byte> {
final Byte unit;
public ByteForm(Byte unit) {
this.unit = unit;
}
@Override
public Byte unit() {
return this.unit;
}
@Override
public Form<Byte> unit(Byte unit) {
return new ByteForm(unit);
}
@Override
public Class<Byte> type() {
return Byte.class;
}
@Override
public Item mold(Byte value) {
if (value != null) {
return Num.from(value.byteValue());
} else {
return Item.extant();
}
}
@Override
public Byte cast(Item item) {
final Value value = item.target();
try {
return value.byteValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/CharacterForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* Character}.
*/
public final class CharacterForm extends Form<Character> {
final Character unit;
public CharacterForm(Character unit) {
this.unit = unit;
}
@Override
public Character unit() {
return this.unit;
}
@Override
public Form<Character> unit(Character unit) {
return new CharacterForm(unit);
}
@Override
public Class<Character> type() {
return Character.class;
}
@Override
public Item mold(Character value) {
if (value != null) {
return Num.from(value.charValue());
} else {
return Item.extant();
}
}
@Override
public Character cast(Item item) {
final Value value = item.target();
try {
return value.charValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ClassForm.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.form;
import java.lang.reflect.Constructor;
import swim.structure.Attr;
import swim.structure.Form;
import swim.structure.FormException;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Text;
import swim.structure.Value;
/**
* For some {@code Class}, a transformation between a structurally typed {@code
* Item} and an instance of that {@code Class}.
* <p>
* A {@code Class} instance in Java can be completely described by its {@code
* Class} <em>name</em>, its <em>field names</em>, and its <em>field
* values</em>. This, combined with the fact that some fields are more
* important than others, gives rise to the following rules for transforming
* objects into structured {@code Items}:
* <ol>
* <li>The {@code} String identifier for the {@code Class}, usually simply
* the {@code Class} name or some function of it, is called {@code tag}.</li>
* <li>Every field being considered is either a {@code member} or a {@code
* header}, with the latter reserved for "important" fields.</li>
* <li>Every such field transforms to a {@link swim.structure.Slot} whose key
* is the name of the field and whose value is the {@code Item} representation
* of the field.</li>
* <li>The structural representation of every object is some {@link Record}
* unless the item is {@code null}, which always transforms into {@link
* swim.structure.Extant}.</li>
* <li>The first entry in this {@code Record} is an {@link Attr} whose key is
* {@code tag} and whose value is either:
* <ul>
* <li>A {@code Record} of {@code headers} that were transformed into {@code
* Slots} via rule 3., or</li>
* <li>{@code Extant} if this {@code Class} has no fields.</li>
* </ul>
* </li>
* <li>Every subsequent entry in the {@code Record} from rule 4. is a {@code
* member} that was transformed into a {@code Slot} via rule 3.</li>
* </ol>
* <p>
* These rules can be directly inverted to transform {@code Items} to objects.
*/
public final class ClassForm<T> extends Form<T> implements Cloneable {
final Class<?> type;
final String tag;
final T unit;
final Constructor<T> constructor;
Form<T>[] headers;
Form<T>[] members;
@SuppressWarnings("unchecked")
ClassForm(Class<?> type, String tag, T unit, Constructor<T> constructor,
Form<T>[] headers, Form<T>[] members) {
this.type = type;
this.tag = tag;
this.unit = unit;
this.constructor = constructor;
this.headers = headers;
this.members = members;
}
@SuppressWarnings("unchecked")
public ClassForm(Class<?> type, String tag, T unit) {
this.type = type;
this.tag = tag;
this.unit = unit;
Constructor<T> constructor;
try {
constructor = (Constructor<T>) type.getDeclaredConstructor();
constructor.setAccessible(true);
} catch (NoSuchMethodException cause) {
constructor = null;
}
this.constructor = constructor;
this.headers = (Form<T>[]) new Form<?>[0];
this.members = (Form<T>[]) new Form<?>[0];
}
@Override
public String tag() {
return this.tag;
}
@Override
public ClassForm<T> tag(String tag) {
return new ClassForm<T>(this.type, tag, this.unit, this.constructor, this.headers, this.members);
}
@Override
public T unit() {
return this.unit;
}
@Override
public ClassForm<T> unit(T unit) {
return new ClassForm<T>(this.type, this.tag, unit, this.constructor, this.headers, this.members);
}
@SuppressWarnings("unchecked")
public ClassForm<T> addHeader(Form<T> header) {
final Form<T>[] oldHeaders = this.headers;
final int n = oldHeaders.length;
final Form<T>[] newHeaders = (Form<T>[]) new Form<?>[n + 1];
System.arraycopy(oldHeaders, 0, newHeaders, 0, n);
newHeaders[n] = header;
this.headers = newHeaders;
return this;
}
@SuppressWarnings("unchecked")
public ClassForm<T> putHeader(Form<T> header) {
if (header instanceof FieldForm<?>) {
final String name = ((FieldForm<?>) header).field().getName();
final Form<T>[] oldHeaders = this.headers;
for (int i = 0, n = oldHeaders.length; i < n; i += 1) {
final Form<T> oldHeader = oldHeaders[i];
if (oldHeader instanceof FieldForm<?> && name.equals(((FieldForm<?>) oldHeader).field().getName())) {
final Form<T>[] newHeaders = (Form<T>[]) new Form<?>[n];
System.arraycopy(oldHeaders, 0, newHeaders, 0, n);
newHeaders[i] = header;
this.headers = newHeaders;
return this;
}
}
}
return addHeader(header);
}
public ClassForm<T> putHeader(String name, Value key, Form<?> form) {
try {
final java.lang.reflect.Field field = this.type.getField(name);
final Form<T> header = new SlotForm<T>(field, key, form);
return putHeader(header);
} catch (NoSuchFieldException cause) {
throw new FormException(cause);
}
}
public ClassForm<T> putHeader(String name, Form<?> form) {
return putHeader(name, Text.from(name), form);
}
@SuppressWarnings("unchecked")
public ClassForm<T> addMember(Form<T> member) {
final Form<T>[] oldMembers = this.members;
final int n = oldMembers.length;
final Form<T>[] newMembers = (Form<T>[]) new Form<?>[n + 1];
System.arraycopy(oldMembers, 0, newMembers, 0, n);
newMembers[n] = member;
this.members = newMembers;
return this;
}
@SuppressWarnings("unchecked")
public ClassForm<T> putMember(Form<T> member) {
if (member instanceof FieldForm<?>) {
final String name = ((FieldForm<?>) member).field().getName();
final Form<T>[] oldMembers = this.members;
for (int i = 0, n = oldMembers.length; i < n; i += 1) {
final Form<T> oldMember = oldMembers[i];
if (oldMember instanceof FieldForm<?> && name.equals(((FieldForm<?>) oldMember).field().getName())) {
final Form<T>[] newMembers = (Form<T>[]) new Form<?>[n];
System.arraycopy(oldMembers, 0, newMembers, 0, n);
newMembers[i] = member;
this.members = newMembers;
return this;
}
}
}
return addMember(member);
}
public ClassForm<T> putMember(String name, Value key, Form<?> form) {
try {
final java.lang.reflect.Field field = this.type.getField(name);
final Form<T> member = new SlotForm<T>(field, key, form);
return putMember(member);
} catch (NoSuchFieldException cause) {
throw new FormException(cause);
}
}
public ClassForm<T> putMember(String name, Form<?> form) {
return putMember(name, Text.from(name), form);
}
@Override
public Class<?> type() {
return this.type;
}
@Override
public Item mold(T object, Item item) {
if (object != null) {
final Item head;
if (this.tag != null) {
Value header = Value.absent();
for (int i = 0, n = this.headers.length; i < n; i += 1) {
header = this.headers[i].mold(object, header).toValue();
}
if (!header.isDefined()) {
header = Value.extant();
}
head = Attr.of(this.tag, header);
} else {
head = null;
}
for (int i = 0, n = this.members.length; i < n; i += 1) {
item = this.members[i].mold(object, item);
}
if (head != null) {
item = item.prepended(head);
}
}
return item;
}
@Override
public Item mold(T object) {
if (object != null) {
final Item head;
if (this.tag != null) {
Value header = Value.absent();
for (int i = 0, n = this.headers.length; i < n; i += 1) {
header = this.headers[i].mold(object, header).toValue();
}
if (!header.isDefined()) {
header = Value.extant();
}
head = Attr.of(this.tag, header);
} else {
head = null;
}
final int n = this.members.length;
Item item = Record.create(head != null ? 1 + n : n);
for (int i = 0; i < n; i += 1) {
item = this.members[i].mold(object, item);
}
if (head != null) {
item = item.prepended(head);
}
return item;
} else {
return Item.extant();
}
}
@Override
public T cast(Item item, T object) {
final Value value = item.toValue();
if (this.tag != null) {
final Value header = value.header(this.tag);
if (!header.isDefined()) {
return null;
}
for (int i = 0, n = this.headers.length; i < n; i += 1) {
object = this.headers[i].cast(header, object);
}
}
for (int i = 0, n = this.members.length; i < n; i += 1) {
object = this.members[i].cast(value, object);
}
return object;
}
@Override
public T cast(Item item) {
if (this.constructor != null) {
try {
final T object = this.constructor.newInstance();
return cast(item, object);
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
}
return null;
}
@Override
public ClassForm<T> clone() {
return new ClassForm<T>(this.type, this.tag, this.unit, this.constructor, this.headers, this.members);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/CollectionForm.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.form;
import java.lang.reflect.Constructor;
import java.util.Collection;
import swim.structure.Form;
import swim.structure.FormException;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
public final class CollectionForm<T> extends Form<Collection<T>> {
final Class<?> type;
final Form<T> form;
final Constructor<Collection<T>> constructor;
@SuppressWarnings("unchecked")
public CollectionForm(Class<?> type, Form<T> form) {
this.type = type;
this.form = form;
try {
this.constructor = (Constructor<Collection<T>>) type.getConstructor();
} catch (NoSuchMethodException cause) {
throw new FormException(cause);
}
}
@Override
public Collection<T> unit() {
try {
return this.constructor.newInstance();
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
}
@Override
public Class<?> type() {
return this.type;
}
@Override
public Item mold(Collection<T> collection, Item item) {
if (collection != null) {
for (T elem : collection) {
item = item.appended(this.form.mold(elem));
}
return item;
} else {
return Item.extant();
}
}
@Override
public Item mold(Collection<T> collection) {
if (collection != null) {
final Record record = Record.create();
for (T elem : collection) {
record.add(this.form.mold(elem));
}
return record;
} else {
return Item.extant();
}
}
@Override
public Collection<T> cast(Item item, Collection<T> collection) {
final Value value = item.toValue();
for (Item child : value) {
final T elem = this.form.cast(child);
if (elem != null) {
collection.add(elem);
}
}
return collection;
}
@Override
public Collection<T> cast(Item item) {
final Value value = item.toValue();
final int n = value.length();
if (n > 0) {
final Collection<T> collection;
try {
collection = this.constructor.newInstance();
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
for (Item child : value) {
final T elem = this.form.cast(child);
if (elem != null) {
collection.add(elem);
}
}
return collection;
} else if (value.isDefined()) {
final T elem = this.form.cast(value);
if (elem != null) {
final Collection<T> collection;
try {
collection = this.constructor.newInstance();
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
collection.add(elem);
return collection;
}
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/DoubleForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* Double}.
*/
public final class DoubleForm extends Form<Double> {
public final Double unit;
public DoubleForm(Double unit) {
this.unit = unit;
}
@Override
public Double unit() {
return this.unit;
}
@Override
public Form<Double> unit(Double unit) {
return new DoubleForm(unit);
}
@Override
public Class<Double> type() {
return Double.class;
}
@Override
public Item mold(Double value) {
if (value != null) {
return Num.from(value.doubleValue());
} else {
return Item.extant();
}
}
@Override
public Double cast(Item item) {
final Value value = item.target();
try {
return value.doubleValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/FieldForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Value;
public abstract class FieldForm<T> extends Form<T> {
public abstract java.lang.reflect.Field field();
public abstract Value key();
@Override
public abstract T cast(Item item, T object);
@Override
public T cast(Item item) {
return cast(item, null);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/FloatForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link Float}.
*/
public final class FloatForm extends Form<Float> {
final Float unit;
public FloatForm(Float unit) {
this.unit = unit;
}
@Override
public Float unit() {
return this.unit;
}
@Override
public Form<Float> unit(Float unit) {
return new FloatForm(unit);
}
@Override
public Class<Float> type() {
return Float.class;
}
@Override
public Item mold(Float value) {
if (value != null) {
return Num.from(value.floatValue());
} else {
return Item.extant();
}
}
@Override
public Float cast(Item item) {
final Value value = item.target();
try {
return value.floatValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/IntegerForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and an {@link
* Integer}.
*/
public final class IntegerForm extends Form<Integer> {
final Integer unit;
public IntegerForm(Integer unit) {
this.unit = unit;
}
@Override
public Integer unit() {
return this.unit;
}
@Override
public Form<Integer> unit(Integer unit) {
return new IntegerForm(unit);
}
@Override
public Class<Integer> type() {
return Integer.class;
}
@Override
public Item mold(Integer value) {
if (value != null) {
return Num.from(value.intValue());
} else {
return Item.extant();
}
}
@Override
public Integer cast(Item item) {
final Value value = item.target();
try {
return value.intValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ItemForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
/**
* Fall-through "transformation" between a structural {@link Item} and a nominal
* {@code Item}. Both {@link #mold} and {@link #cast} simply return the {@code
* Item} argument itself.
*/
public final class ItemForm extends Form<Item> {
final Item unit;
public ItemForm(Item unit) {
this.unit = unit != null ? unit.commit() : unit;
}
@Override
public Item unit() {
return this.unit;
}
@Override
public Form<Item> unit(Item unit) {
return new ItemForm(unit);
}
@Override
public Class<Item> type() {
return Item.class;
}
@Override
public Item mold(Item item) {
if (item != null) {
return item;
} else {
return Item.extant();
}
}
@Override
public Item cast(Item item) {
return item;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/LongForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link Long}.
*/
public final class LongForm extends Form<Long> {
final Long unit;
public LongForm(Long unit) {
this.unit = unit;
}
@Override
public Long unit() {
return this.unit;
}
@Override
public Form<Long> unit(Long unit) {
return new LongForm(unit);
}
@Override
public Class<Long> type() {
return Long.class;
}
@Override
public Item mold(Long value) {
if (value != null) {
return Num.from(value.longValue());
} else {
return Item.extant();
}
}
@Override
public Long cast(Item item) {
final Value value = item.target();
try {
return value.longValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/MapForm.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.form;
import java.lang.reflect.Constructor;
import java.util.Map;
import swim.structure.Field;
import swim.structure.Form;
import swim.structure.FormException;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
/**
* For some {@link Map Map<K,V>}, a transformation between a structurally
* typed {@code Item} and an instance of that {@code Map}.
*/
public final class MapForm<K, V> extends Form<Map<K, V>> {
final Class<?> type;
final Form<K> keyForm;
final Form<V> valForm;
final Constructor<Map<K, V>> constructor;
@SuppressWarnings("unchecked")
public MapForm(Class<?> type, Form<K> keyForm, Form<V> valForm) {
this.type = type;
this.keyForm = keyForm;
this.valForm = valForm;
try {
this.constructor = (Constructor<Map<K, V>>) type.getConstructor();
} catch (NoSuchMethodException cause) {
throw new FormException(cause);
}
}
@Override
public Map<K, V> unit() {
try {
return this.constructor.newInstance();
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
}
@Override
public Class<?> type() {
return this.type;
}
@Override
public Item mold(Map<K, V> map, Item item) {
if (map != null) {
for (Map.Entry<K, V> entry : map.entrySet()) {
final Value key = this.keyForm.mold(entry.getKey()).toValue();
final Value val = this.valForm.mold(entry.getValue()).toValue();
item = item.updatedSlot(key, val);
}
return item;
} else {
return Item.extant();
}
}
@Override
public Item mold(Map<K, V> map) {
if (map != null) {
final Record record = Record.create();
for (Map.Entry<K, V> entry : map.entrySet()) {
final Value key = this.keyForm.mold(entry.getKey()).toValue();
final Value val = this.valForm.mold(entry.getValue()).toValue();
record.slot(key, val);
}
return record;
} else {
return Item.extant();
}
}
@Override
public Map<K, V> cast(Item item, Map<K, V> map) {
final Value value = item.toValue();
for (Item child : value) {
if (child instanceof Field) {
final K key = this.keyForm.cast(child.key());
if (key != null) {
final V val = this.valForm.cast(child.toValue());
if (val != null) {
map.put(key, val);
}
}
}
}
return map;
}
@Override
public Map<K, V> cast(Item item) {
final Value value = item.toValue();
final int n = value.length();
if (value instanceof Record) {
final Map<K, V> map;
try {
map = this.constructor.newInstance();
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
for (Item child : value) {
if (child instanceof Field) {
final K key = this.keyForm.cast(child.key());
if (key != null) {
final V val = this.valForm.cast(child.toValue());
if (val != null) {
map.put(key, val);
}
}
}
}
return map;
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/NumberForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* Number}.
*/
public final class NumberForm extends Form<Number> {
final Number unit;
public NumberForm(Number unit) {
this.unit = unit;
}
@Override
public Number unit() {
return this.unit;
}
@Override
public Form<Number> unit(Number unit) {
return new NumberForm(unit);
}
@Override
public Class<Number> type() {
return Number.class;
}
@Override
public Item mold(Number value) {
if (value != null) {
return Num.from(value);
} else {
return Item.extant();
}
}
@Override
public Number cast(Item item) {
final Value value = item.target();
try {
return value.numberValue(null);
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/PolyForm.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.form;
import java.lang.reflect.Field;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import swim.collections.HashTrieMap;
import swim.structure.Bool;
import swim.structure.Data;
import swim.structure.Form;
import swim.structure.FormException;
import swim.structure.Header;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Member;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Tag;
import swim.structure.Text;
public class PolyForm extends Form<Object> implements Cloneable {
final Object unit;
HashTrieMap<Class<?>, Form<?>> classForms;
HashTrieMap<String, Form<?>> tagForms;
PolyForm(Object unit, HashTrieMap<Class<?>, Form<?>> classForms,
HashTrieMap<String, Form<?>> tagForms) {
this.unit = unit;
this.classForms = classForms;
this.tagForms = tagForms;
}
public PolyForm() {
this(null, HashTrieMap.<Class<?>, Form<?>>empty(), HashTrieMap.<String, Form<?>>empty());
}
@Override
public final Object unit() {
return this.unit;
}
@Override
public PolyForm unit(Object unit) {
return new PolyForm(unit, this.classForms, this.tagForms);
}
@Override
public final Class<?> type() {
return Object.class;
}
@Override
public Item mold(Object object, Item item) {
if (object != null) {
final Form<Object> form = formForClass(object.getClass());
if (form != null) {
return form.mold(object, item);
} else if (object instanceof String) {
return moldString((String) object, item);
} else if (object instanceof Number) {
return moldNumber((Number) object, item);
} else if (object instanceof Character) {
return moldCharacter((Character) object, item);
} else if (object instanceof Boolean) {
return moldBoolean((Boolean) object, item);
} else if (object instanceof ByteBuffer) {
return moldByteBuffer((ByteBuffer) object, item);
} else if (object instanceof Map<?, ?>) {
return moldMap((Map<?, ?>) object, item);
} else if (object instanceof Collection<?>) {
return moldCollection((Collection<?>) object, item);
} else if (object instanceof Object[]) {
return moldArray((Object[]) object, item);
} else {
return Item.absent();
}
} else {
return Item.extant();
}
}
@Override
public Item mold(Object object) {
if (object != null) {
final Form<Object> form = formForClass(object.getClass());
if (form != null) {
return form.mold(object);
} else if (object instanceof String) {
return moldString((String) object);
} else if (object instanceof Number) {
return moldNumber((Number) object);
} else if (object instanceof Character) {
return moldCharacter((Character) object);
} else if (object instanceof Boolean) {
return moldBoolean((Boolean) object);
} else if (object instanceof ByteBuffer) {
return moldByteBuffer((ByteBuffer) object);
} else if (object instanceof Map<?, ?>) {
return moldMap((Map<?, ?>) object);
} else if (object instanceof Collection<?>) {
return moldCollection((Collection<?>) object);
} else if (object instanceof Object[]) {
return moldArray((Object[]) object);
} else {
return Item.absent();
}
} else {
return Item.extant();
}
}
protected Item moldString(String object, Item item) {
return Form.forString().mold(object, item);
}
protected Item moldString(String object) {
return Form.forString().mold(object);
}
protected Item moldNumber(Number object, Item item) {
return Form.forNumber().mold(object, item);
}
protected Item moldNumber(Number object) {
return Form.forNumber().mold(object);
}
protected Item moldCharacter(Character object, Item item) {
return Form.forCharacter().mold(object, item);
}
protected Item moldCharacter(Character object) {
return Form.forCharacter().mold(object);
}
protected Item moldBoolean(Boolean object, Item item) {
return Form.forBoolean().mold(object, item);
}
protected Item moldBoolean(Boolean object) {
return Form.forBoolean().mold(object);
}
protected Item moldByteBuffer(ByteBuffer object, Item item) {
return Form.forByteBuffer().mold(object, item);
}
protected Item moldByteBuffer(ByteBuffer object) {
return Form.forByteBuffer().mold(object);
}
protected Item moldArray(Object[] object, Item item) {
return Form.forArray(object.getClass().getComponentType(), this).mold(object, item);
}
protected Item moldArray(Object[] object) {
return Form.forArray(object.getClass().getComponentType(), this).mold(object);
}
protected Item moldCollection(Collection<?> object, Item item) {
return Form.forCollection(object.getClass(), this).mold(object, item);
}
protected Item moldCollection(Collection<?> object) {
return Form.forCollection(object.getClass(), this).mold(object);
}
protected Item moldMap(Map<?, ?> object, Item item) {
return Form.forMap(object.getClass(), this, this).mold(object, item);
}
protected Item moldMap(Map<?, ?> object) {
return Form.forMap(object.getClass(), this, this).mold(object);
}
@Override
public Object cast(Item item, Object object) {
if (item instanceof Record) {
return castRecord((Record) item, object);
} else if (item instanceof Text) {
return castText((Text) item);
} else if (item instanceof Data) {
return castData((Data) item);
} else if (item instanceof Num) {
return castNum((Num) item);
} else if (item instanceof Bool) {
return castBool((Bool) item);
}
return null;
}
@Override
public Object cast(Item item) {
if (item instanceof Record) {
return castRecord((Record) item);
} else if (item instanceof Text) {
return castText((Text) item);
} else if (item instanceof Data) {
return castData((Data) item);
} else if (item instanceof Num) {
return castNum((Num) item);
} else if (item instanceof Bool) {
return castBool((Bool) item);
}
return null;
}
protected Object castRecord(Record value, Object object) {
final Form<Object> form = formForTag(value.tag());
if (form != null) {
return form.cast(value, object);
} else {
return null;
}
}
protected Object castRecord(Record value) {
final Form<Object> form = formForTag(value.tag());
if (form != null) {
return form.cast(value);
} else {
return null;
}
}
protected Object castText(Text value) {
return Form.forString().cast(value);
}
protected Object castData(Data value) {
return Form.forByteBuffer().cast(value);
}
protected Object castNum(Num value) {
return Form.forNumber().cast(value);
}
protected Object castBool(Bool value) {
return Form.forBoolean().cast(value);
}
@SuppressWarnings("unchecked")
public <T> Form<T> formForTag(String tag) {
if (tag != null) {
return (Form<T>) this.tagForms.get(tag);
}
return null;
}
@SuppressWarnings("unchecked")
public <T> Form<T> formForClass(Class<?> type) {
Form<T> form;
do {
form = (Form<T>) this.classForms.get(type);
type = type.getSuperclass();
} while (form == null && type != null);
return form;
}
@SuppressWarnings("unchecked")
public <T> Form<T> formForType(Type genericType) {
if (genericType instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
if (componentType instanceof TypeVariable) {
componentType = ((TypeVariable) componentType).getBounds()[0];
}
if (componentType instanceof Class<?>) {
return (Form<T>) Form.forArray((Class<?>) componentType, this);
}
}
if (genericType instanceof ParameterizedType) {
genericType = ((ParameterizedType) genericType).getRawType();
}
if (genericType instanceof Class<?>) {
final Class<Object> type = (Class<Object>) genericType;
if (type.isArray()) {
return (Form<T>) Form.forArray(type.getComponentType(), this);
} else if (Map.class.isAssignableFrom(type)) {
return (Form<T>) Form.forMap(type, this, this);
} else if (Collection.class.isAssignableFrom(type)) {
return (Form<T>) Form.forCollection(type, this);
} else {
Form<T> form = formForClass(type);
if (form != null) {
return form;
}
form = Form.forClass(type);
if (form != null) {
return form;
}
}
}
return (Form<T>) this;
}
public PolyForm addForm(Form<?> newForm) {
if (newForm instanceof PolyForm) {
final PolyForm that = (PolyForm) newForm;
final Iterator<Map.Entry<Class<?>, Form<?>>> classFormsIterator = that.classForms.iterator();
while (classFormsIterator.hasNext()) {
final Map.Entry<Class<?>, Form<?>> entry = classFormsIterator.next();
this.classForms = this.classForms.updated(entry.getKey(), entry.getValue());
}
final Iterator<Map.Entry<String, Form<?>>> tagFormsIterator = that.tagForms.iterator();
while (tagFormsIterator.hasNext()) {
final Map.Entry<String, Form<?>> entry = tagFormsIterator.next();
this.tagForms = this.tagForms.updated(entry.getKey(), entry.getValue());
}
} else {
final Class<?> newClass = newForm.type();
if (!classForms.containsKey(newClass)) {
final String newTag = newForm.tag();
if (newTag != null) {
classForms = classForms.updated(newClass, newForm);
tagForms = tagForms.updated(newTag, newForm);
}
}
}
return this;
}
public PolyForm addForms(Form<?>... newForms) {
for (int i = 0, n = newForms.length; i < n; i += 1) {
addForm(newForms[i]);
}
return this;
}
public PolyForm addClass(Class<?> newClass) {
if (!classForms.containsKey(newClass)) {
final Form<?> newForm = Form.forClass(newClass);
if (newForm != null) {
final String newTag = newForm.tag();
if (newTag != null) {
classForms = classForms.updated(newClass, newForm);
tagForms = tagForms.updated(newTag, newForm);
}
}
}
return this;
}
public PolyForm addClasses(Class<?>... newClasses) {
for (int i = 0, n = newClasses.length; i < n; i += 1) {
addClass(newClasses[i]);
}
return this;
}
public PolyForm addType(Type genericType) {
if (genericType instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) genericType).getGenericComponentType();
if (componentType instanceof TypeVariable) {
componentType = ((TypeVariable) componentType).getBounds()[0];
}
if (componentType instanceof Class<?>) {
addClass((Class<?>) componentType);
}
} else if (genericType instanceof ParameterizedType) {
addTypes(((ParameterizedType) genericType).getActualTypeArguments());
} else if (genericType instanceof Class<?>) {
final Class<?> type = (Class<?>) genericType;
if (type.isArray()) {
addClass(type.getComponentType());
} else {
addClass(type);
}
}
return this;
}
public PolyForm addTypes(Type... newGenericTypes) {
for (int i = 0, n = newGenericTypes.length; i < n; i += 1) {
addType(newGenericTypes[i]);
}
return this;
}
public <T> ClassForm<T> reflectClassForm(ClassForm<T> classForm) {
addForm(classForm);
reflectFields(classForm, classForm.type());
return classForm;
}
public <T> ClassForm<T> reflectClassForm(Class<?> type, String tag, T unit) {
if (!type.isInterface() && (type.getModifiers() & Modifier.ABSTRACT) == 0 && !Form.isBuiltin(type)) {
return reflectClassForm(new ClassForm<T>(type, tag, unit));
}
return null;
}
public <T> ClassForm<T> reflectClassForm(Class<?> type, String tag) {
return reflectClassForm(type, tag, null);
}
public <T> ClassForm<T> reflectClassForm(Class<?> type) {
final String tag = reflectClassTag(type);
return reflectClassForm(type, tag, null);
}
@SuppressWarnings("unchecked")
public <T> Form<T> reflectClass(Class<?> type) {
if (!type.isInterface() && (type.getModifiers() & Modifier.ABSTRACT) == 0 && !Form.isBuiltin(type)) {
final Field[] fields = type.getDeclaredFields();
for (int i = 0, n = fields.length; i < n; i += 1) {
final Field field = fields[i];
final Kind kind = field.getAnnotation(Kind.class);
if (kind != null) {
if (!Form.class.isAssignableFrom(field.getType())) {
throw new FormException(field.toString());
}
final int modifiers = field.getModifiers();
if ((modifiers & Modifier.STATIC) == 0) {
throw new FormException(field.toString());
}
field.setAccessible(true);
try {
Form<T> form = (Form<T>) field.get(null);
if (form == null) {
form = reflectClassForm(type);
field.set(null, form);
}
return form;
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
}
}
final Method[] methods = type.getDeclaredMethods();
for (int i = 0, n = methods.length; i < n; i += 1) {
final Method method = methods[i];
final Kind kind = method.getAnnotation(Kind.class);
if (kind != null) {
if (!Form.class.isAssignableFrom(method.getReturnType())) {
throw new FormException(method.toString());
}
if (method.getParameterTypes().length != 0) {
throw new FormException(method.toString());
}
final int modifiers = method.getModifiers();
if ((modifiers & Modifier.STATIC) == 0) {
throw new FormException(method.toString());
}
method.setAccessible(true);
try {
return (Form<T>) method.invoke(null);
} catch (ReflectiveOperationException cause) {
throw new FormException(cause);
}
}
}
return reflectClassForm(type);
}
return null;
}
public <T> Form<T> reflectClassName(String className) {
try {
return reflectClass(Class.forName(className));
} catch (ClassNotFoundException cause) {
throw new FormException(cause);
}
}
public String reflectClassTag(Class<?> type) {
final Tag tag = type.getAnnotation(Tag.class);
if (tag != null) {
return tag.value();
} else {
return type.getSimpleName();
}
}
public <T> ClassForm<T> reflectField(ClassForm<T> classForm, Field field) {
final int modifiers = field.getModifiers();
if ((modifiers & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) {
if ((modifiers & (Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED)) != 0
|| modifiers == 0) {
field.setAccessible(true);
}
// Close over the type generic type of the field.
final Type fieldType = field.getGenericType();
addType(field.getGenericType());
String name;
final FieldForm<T> fieldForm;
final Header header = field.getAnnotation(Header.class);
if (header != null) {
name = header.value();
if (name == null || name.length() == 0) {
name = field.getName();
}
fieldForm = new SlotForm<T>(field, Text.from(name), formForType(fieldType));
return classForm.putHeader(fieldForm);
} else {
final Member member = field.getAnnotation(Member.class);
name = member != null ? member.value() : null;
if (name == null || name.length() == 0) {
name = field.getName();
}
fieldForm = new SlotForm<T>(field, Text.from(name), formForType(fieldType));
return classForm.putMember(fieldForm);
}
}
return classForm;
}
public <T> ClassForm<T> reflectFields(ClassForm<T> classForm, Class<?> type) {
if (type != null) {
reflectFields(classForm, type.getSuperclass());
final Field[] fields = type.getDeclaredFields();
for (int i = 0, n = fields.length; i < n; i += 1) {
reflectField(classForm, fields[i]);
}
}
return classForm;
}
@Override
public PolyForm clone() {
return new PolyForm(this.unit, this.classForms, this.tagForms);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ShortForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link Short}.
*/
public final class ShortForm extends Form<Short> {
public final Short unit;
public ShortForm(Short unit) {
this.unit = unit;
}
@Override
public Short unit() {
return this.unit;
}
@Override
public Form<Short> unit(Short unit) {
return new ShortForm(unit);
}
@Override
public Class<Short> type() {
return Short.class;
}
@Override
public Item mold(Short value) {
if (value != null) {
return Num.from(value.shortValue());
} else {
return Item.extant();
}
}
@Override
public Short cast(Item item) {
final Value value = item.target();
try {
return value.shortValue();
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/SlotForm.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.form;
import swim.structure.Field;
import swim.structure.Form;
import swim.structure.FormException;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Slot;
import swim.structure.Value;
public final class SlotForm<T> extends FieldForm<T> {
final java.lang.reflect.Field field;
final Value key;
final Form<?> form;
public SlotForm(java.lang.reflect.Field field, Value key, Form<?> form) {
this.field = field;
this.key = key.commit();
this.form = form;
}
@Override
public java.lang.reflect.Field field() {
return this.field;
}
@Override
public Value key() {
return this.key;
}
@Override
public Class<?> type() {
return this.field.getDeclaringClass();
}
@SuppressWarnings("unchecked")
@Override
public Item mold(T object, Item item) {
if (object != null) {
try {
final Value value = ((Form<Object>) this.form).mold(this.field.get(object)).toValue();
if (item instanceof Field) {
return ((Field) item).updatedValue(value);
} else {
return item.updatedSlot(this.key, value);
}
} catch (IllegalAccessException cause) {
return Item.absent();
}
} else {
return Item.extant();
}
}
@SuppressWarnings("unchecked")
@Override
public Item mold(T object) {
if (object != null) {
try {
final Value value = ((Form<Object>) this.form).mold(this.field.get(object)).toValue();
return Slot.of(this.key, value);
} catch (IllegalAccessException cause) {
return Item.absent();
}
} else {
return Item.extant();
}
}
@Override
public T cast(Item item, T object) {
try {
if (item instanceof Record) {
final Value value = item.get(this.key);
if (value.isDefined()) {
this.field.set(object, this.form.cast(value));
}
} else if (item.keyEquals(this.key)) {
this.field.set(object, this.form.cast(item.toValue()));
}
} catch (IllegalAccessException cause) {
throw new FormException(cause);
}
return object;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/StringForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Text;
import swim.structure.Value;
/**
* Transformation between a structurally typed {@link Item} and a {@link
* String}.
*/
public final class StringForm extends Form<String> {
final String unit;
public StringForm(String unit) {
this.unit = unit;
}
@Override
public String unit() {
return this.unit;
}
@Override
public Form<String> unit(String unit) {
return new StringForm(unit);
}
@Override
public Class<String> type() {
return String.class;
}
@Override
public Item mold(String value) {
if (value != null) {
return Text.from(value);
} else {
return Item.extant();
}
}
@Override
public String cast(Item item) {
final Value value = item.target();
try {
return value.stringValue(null);
} catch (UnsupportedOperationException e) {
return null;
}
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/TagForm.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.form;
import swim.structure.Attr;
import swim.structure.Form;
import swim.structure.Item;
public final class TagForm<T> extends Form<T> {
final String tag;
final Form<T> form;
public TagForm(String tag, Form<T> form) {
this.tag = tag;
this.form = form;
}
@Override
public String tag() {
return this.tag;
}
@Override
public Form<T> tag(String tag) {
if (tag != null) {
return new TagForm<T>(tag, this.form);
} else {
return this.form;
}
}
@Override
public T unit() {
return this.form.unit();
}
@Override
public Form<T> unit(T unit) {
return new TagForm<T>(this.tag, this.form.unit(unit));
}
@Override
public Class<?> type() {
return this.form.type();
}
@Override
public Item mold(T object, Item item) {
item = this.form.mold(object, item);
if (!item.header(this.tag).isDefined()) {
item = item.prepended(Attr.of(this.tag));
}
return item;
}
@Override
public Item mold(T object) {
Item item = this.form.mold(object);
if (!item.header(this.tag).isDefined()) {
item = item.prepended(Attr.of(this.tag));
}
return item;
}
@Override
public T cast(Item item, T object) {
if (item.header(this.tag).isDefined()) {
return this.form.cast(item, object);
} else if (item.keyEquals(this.tag)) {
return this.form.cast(item.toValue(), object);
}
return null;
}
@Override
public T cast(Item item) {
if (item.header(this.tag).isDefined()) {
return this.form.cast(item);
} else if (item.keyEquals(this.tag)) {
return this.form.cast(item.toValue());
}
return null;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/UnitForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
public final class UnitForm<T> extends Form<T> {
final T unit;
final Form<T> form;
public UnitForm(T unit, Form<T> form) {
this.unit = unit;
this.form = form;
}
@Override
public String tag() {
return this.form.tag();
}
@Override
public Form<T> tag(String tag) {
return new UnitForm<T>(this.unit, this.form.tag(tag));
}
@Override
public T unit() {
return this.unit;
}
@Override
public Form<T> unit(T unit) {
if (unit != null) {
return new UnitForm<T>(unit, this.form);
} else {
return this.form;
}
}
@Override
public Class<?> type() {
return this.form.type();
}
@Override
public Item mold(T object, Item item) {
return this.form.mold(object, item);
}
@Override
public Item mold(T object) {
return this.form.mold(object);
}
@Override
public T cast(Item item, T object) {
return this.form.cast(item, object);
}
@Override
public T cast(Item item) {
return this.form.cast(item);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/ValueForm.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.form;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Value;
/**
* Fall-through transformation between a structurally typed {@link Item} and a
* structurally typed {@link Value}. {@link #mold} simply returns the
* {@code Value} argument itself, and {@link #cast} simply invokes {@link
* Item#toValue()} against the {@code Item} argument.
*/
public final class ValueForm extends Form<Value> {
final Value unit;
public ValueForm(Value unit) {
this.unit = unit != null ? unit.commit() : unit;
}
@Override
public Value unit() {
return this.unit;
}
@Override
public Form<Value> unit(Value unit) {
return new ValueForm(unit);
}
@Override
public Class<Value> type() {
return Value.class;
}
@Override
public Item mold(Value value) {
if (value != null) {
return value;
} else {
return Item.extant();
}
}
@Override
public Value cast(Item item) {
return item.toValue();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/form/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.
/**
* Structural type transformation forms.
*/
package swim.structure.form;
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/func/BridgeFunc.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.func;
import swim.codec.Output;
import swim.structure.Func;
import swim.structure.Item;
public abstract class BridgeFunc extends Func {
@Override
public int typeOrder() {
return 51;
}
@Override
public int compareTo(Item other) {
if (other instanceof BridgeFunc) {
return compareTo((BridgeFunc) other);
}
return Integer.compare(typeOrder(), other.typeOrder());
}
int compareTo(BridgeFunc that) {
return getClass().getName().compareTo(that.getClass().getName());
}
@Override
public boolean equals(Object other) {
return this == other;
}
@Override
public int hashCode() {
return System.identityHashCode(this);
}
@Override
public void debug(Output<?> output) {
output = output.write(getClass().getName()).write('@').write(Integer.toHexString(hashCode()));
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/func/LambdaFunc.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.func;
import swim.codec.Output;
import swim.structure.Func;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Slot;
import swim.structure.Text;
import swim.structure.Value;
import swim.structure.operator.InvokeOperator;
import swim.util.Murmur3;
public class LambdaFunc extends Func {
final Value bindings;
final Value template;
public LambdaFunc(Value bindings, Value template) {
this.bindings = bindings;
this.template = template;
}
public final Value bindings() {
return this.bindings;
}
public final Value template() {
return this.template;
}
@Override
public int precedence() {
return 1;
}
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
final Value bindings = this.bindings;
final int arity = Math.max(1, bindings.length());
final Record params = Record.create(arity);
int i = 0;
int j = 0;
while (i < arity) {
final Item binding = bindings instanceof Record ? bindings.getItem(i) : i == 0 ? bindings : Item.absent();
final Value arg = args instanceof Record ? args.getItem(j).toValue() : j == 0 ? args : Value.absent();
if (binding instanceof Text && arg.isDistinct()) {
params.add(Slot.of((Text) binding, arg));
j += 1;
} else if (binding instanceof Slot) {
if (arg.isDistinct()) {
params.add(((Slot) binding).updatedValue(arg));
} else {
params.add(binding);
}
j += 1;
}
i += 1;
}
interpreter.pushScope(params);
final Item result = this.template.evaluate(interpreter);
interpreter.popScope();
return result;
}
@Override
public int typeOrder() {
return 50;
}
@Override
public int compareTo(Item other) {
if (other instanceof LambdaFunc) {
return compareTo((LambdaFunc) other);
}
return Integer.compare(typeOrder(), other.typeOrder());
}
int compareTo(LambdaFunc that) {
int order = this.bindings.compareTo(that.bindings);
if (order == 0) {
order = this.template.compareTo(that.template);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof LambdaFunc) {
final LambdaFunc that = (LambdaFunc) other;
return this.bindings.equals(that.bindings) && this.template.equals(that.template);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(LambdaFunc.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.bindings.hashCode()), this.template.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.bindings).write('.').write("lambda").write('(').debug(this.template).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/func/MathModule.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.func;
import swim.structure.Func;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Value;
import swim.structure.operator.InvokeOperator;
public final class MathModule {
private MathModule() {
// stub
}
private static Func max;
private static Func min;
private static Func abs;
private static Func ceil;
private static Func floor;
private static Func round;
private static Func sqrt;
private static Func pow;
private static Func rate;
private static Func random;
private static Record scope;
public static Func max() {
if (max == null) {
max = new MaxFunc();
}
return max;
}
public static Func min() {
if (min == null) {
min = new MinFunc();
}
return min;
}
public static Func abs() {
if (abs == null) {
abs = new AbsFunc();
}
return abs;
}
public static Func ceil() {
if (ceil == null) {
ceil = new CeilFunc();
}
return ceil;
}
public static Func floor() {
if (floor == null) {
floor = new FloorFunc();
}
return floor;
}
public static Func round() {
if (round == null) {
round = new RoundFunc();
}
return round;
}
public static Func sqrt() {
if (sqrt == null) {
sqrt = new SqrtFunc();
}
return sqrt;
}
public static Func pow() {
if (pow == null) {
pow = new PowFunc();
}
return pow;
}
public static Func rate() {
if (rate == null) {
rate = new RateFunc();
}
return rate;
}
public static Func random() {
if (random == null) {
random = new RandomFunc();
}
return random;
}
public static Record scope() {
if (scope == null) {
scope = Record.create(10)
.slot("max", max())
.slot("min", min())
.slot("abs", abs())
.slot("ceil", ceil())
.slot("floor", floor())
.slot("round", round())
.slot("sqrt", sqrt())
.slot("pow", pow())
.slot("rate", rate())
.slot("random", random())
.commit();
}
return scope;
}
}
final class MaxFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
final Item x;
Item y;
if (args.length() >= 2) {
x = args.getItem(0).evaluate(interpreter);
y = args.getItem(1).evaluate(interpreter);
} else {
x = args.evaluate(interpreter);
y = null;
}
if (y != null) {
return x.max(y);
} else if (operator != null) {
y = (Item) operator.state();
final Item max = y != null ? x.max(y) : x;
operator.setState(max);
return max;
}
return Item.absent();
}
@Override
public Item expand(Value args, Interpreter interpreter, InvokeOperator operator) {
if (args.length() == 1) {
args = args.evaluate(interpreter).toValue();
return invoke(args, interpreter, operator);
}
return null;
}
}
final class MinFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
final Item x;
Item y;
if (args.length() >= 2) {
x = args.getItem(0).evaluate(interpreter);
y = args.getItem(1).evaluate(interpreter);
} else {
x = args.evaluate(interpreter);
y = null;
}
if (y != null) {
return x.min(y);
} else if (operator != null) {
y = (Item) operator.state();
final Item min = y != null ? x.min(y) : x;
operator.setState(min);
return min;
}
return Item.absent();
}
@Override
public Item expand(Value args, Interpreter interpreter, InvokeOperator operator) {
if (args.length() == 1) {
args = args.evaluate(interpreter).toValue();
return invoke(args, interpreter, operator);
}
return null;
}
}
final class AbsFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
if (args instanceof Num) {
return ((Num) args).abs();
}
return Item.absent();
}
}
final class CeilFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
if (args instanceof Num) {
return ((Num) args).ceil();
}
return Item.absent();
}
}
final class FloorFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
if (args instanceof Num) {
return ((Num) args).floor();
}
return Item.absent();
}
}
final class RoundFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
if (args instanceof Num) {
return ((Num) args).round();
}
return Item.absent();
}
}
final class SqrtFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
if (args instanceof Num) {
return ((Num) args).sqrt();
}
return Item.absent();
}
}
final class PowFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
final Value x = args.getItem(0).evaluate(interpreter).toValue();
final Value y = args.getItem(1).evaluate(interpreter).toValue();
if (x instanceof Num && y instanceof Num) {
return ((Num) x).pow((Num) y);
}
return Item.absent();
}
}
final class RateFuncState {
double v0;
long t0;
double dv;
long dt;
}
final class RateFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
final double value;
final long period;
if (args.length() >= 2) {
value = args.getItem(0).evaluate(interpreter).doubleValue(Double.NaN);
period = args.getItem(1).evaluate(interpreter).longValue(1000L);
} else {
value = args.evaluate(interpreter).doubleValue(Double.NaN);
period = 1000L;
}
if (!Double.isNaN(value) && operator != null) {
RateFuncState state = (RateFuncState) operator.state();
if (state == null) {
state = new RateFuncState();
state.v0 = value;
state.t0 = System.currentTimeMillis();
operator.setState(state);
} else {
final long t1 = System.currentTimeMillis();
final long dt = t1 - state.t0;
if (dt > period && t1 > state.t0 || dt > 2 * period) {
final double dv = value - state.v0;
state.v0 = value;
state.t0 = t1;
state.dv = dv;
state.dt = dt;
}
operator.setState(state);
if (state.dt != 0L) {
final double rate = (double) period * state.dv / (double) state.dt;
return Num.from(rate);
}
}
}
return Item.absent();
}
@Override
public Item expand(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
return invoke(args, interpreter, operator);
}
}
final class RandomFunc extends BridgeFunc {
@Override
public Item invoke(Value args, Interpreter interpreter, InvokeOperator operator) {
args = args.evaluate(interpreter).toValue();
final double lower = args.length() >= 1 ? args.getItem(0).doubleValue(0.0) : 0.0;
final double upper = args.length() >= 2 ? args.getItem(1).doubleValue(lower + 1.0) : lower + 1.0;
final double value = lower + Math.random() * (upper - lower);
return Num.from(value);
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/func/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.
/**
* Function expression trees.
*/
package swim.structure.func;
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/AndOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class AndOperator extends BinaryOperator {
public AndOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "&&";
}
@Override
public int precedence() {
return 4;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item result;
final Item argument1 = this.operand1.evaluate(interpreter);
if (argument1.booleanValue(false)) {
final Item argument2 = this.operand2.evaluate(interpreter);
result = argument2;
} else {
result = argument1;
}
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.and(argument2);
}
@Override
public int typeOrder() {
return 22;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof AndOperator) {
return compareTo((AndOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(AndOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof AndOperator) {
final AndOperator that = (AndOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(AndOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("and").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/BinaryOperator.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.operator;
import swim.structure.Item;
import swim.structure.Operator;
/**
* An {@link Operator} that represents a binary operation, i.e. an operation on
* two operands.
*/
public abstract class BinaryOperator extends Operator {
final Item operand1;
final Item operand2;
public BinaryOperator(Item operand1, Item operand2) {
this.operand1 = operand1.commit();
this.operand2 = operand2.commit();
}
public final Item operand1() {
return this.operand1;
}
/**
* Returns the token that identifiers this {@code Operator}'s operation.
* Used to uniquely identify the type of operation, and to aid serialization.
*/
public abstract String operator();
public final Item operand2() {
return this.operand2;
}
@Override
public boolean isConstant() {
return this.operand1.isConstant() && this.operand2.isConstant();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/BitwiseAndOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class BitwiseAndOperator extends BinaryOperator {
public BitwiseAndOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "&";
}
@Override
public int precedence() {
return 7;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.bitwiseAnd(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.bitwiseAnd(argument2);
}
@Override
public int typeOrder() {
return 25;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof BitwiseAndOperator) {
return compareTo((BitwiseAndOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(BitwiseAndOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof BitwiseAndOperator) {
final BitwiseAndOperator that = (BitwiseAndOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(BitwiseAndOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("bitwiseAnd").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/BitwiseNotOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class BitwiseNotOperator extends UnaryOperator {
public BitwiseNotOperator(Item operand) {
super(operand);
}
@Override
public String operator() {
return "~";
}
@Override
public int precedence() {
return 10;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument = this.operand.evaluate(interpreter);
final Item result = argument.bitwiseNot();
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument = this.operand.substitute(interpreter);
return argument.bitwiseNot();
}
@Override
public int typeOrder() {
return 38;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof BitwiseNotOperator) {
return compareTo((BitwiseNotOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(BitwiseNotOperator that) {
return this.operand.compareTo(that.operand);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof BitwiseNotOperator) {
final BitwiseNotOperator that = (BitwiseNotOperator) other;
return this.operand.equals(that.operand);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(BitwiseNotOperator.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.operand.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand).write('.').write("bitwiseNot").write('(').write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/BitwiseOrOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class BitwiseOrOperator extends BinaryOperator {
public BitwiseOrOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "|";
}
@Override
public int precedence() {
return 5;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.bitwiseOr(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.bitwiseOr(argument2);
}
@Override
public int typeOrder() {
return 23;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof BitwiseOrOperator) {
return compareTo((BitwiseOrOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(BitwiseOrOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof BitwiseOrOperator) {
final BitwiseOrOperator that = (BitwiseOrOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(BitwiseOrOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("bitwiseOr").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/BitwiseXorOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class BitwiseXorOperator extends BinaryOperator {
public BitwiseXorOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "^";
}
@Override
public int precedence() {
return 6;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.bitwiseXor(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.bitwiseXor(argument2);
}
@Override
public int typeOrder() {
return 24;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof BitwiseXorOperator) {
return compareTo((BitwiseXorOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(BitwiseXorOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof BitwiseXorOperator) {
final BitwiseXorOperator that = (BitwiseXorOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(BitwiseXorOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("bitwiseXor").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/ConditionalOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class ConditionalOperator extends Operator {
final Item ifTerm;
final Item thenTerm;
final Item elseTerm;
public ConditionalOperator(Item ifTerm, Item thenTerm, Item elseTerm) {
this.ifTerm = ifTerm.commit();
this.thenTerm = thenTerm.commit();
this.elseTerm = elseTerm.commit();
}
public Item ifTerm() {
return this.ifTerm;
}
public Item thenTerm() {
return this.thenTerm;
}
public Item elseTerm() {
return this.elseTerm;
}
@Override
public boolean isConstant() {
return this.ifTerm.isConstant() && this.thenTerm.isConstant()
&& this.elseTerm.isConstant();
}
@Override
public int precedence() {
return 2;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item result;
final Item ifTerm = this.ifTerm.evaluate(interpreter);
if (ifTerm.booleanValue(false)) {
final Item thenTerm = this.thenTerm.evaluate(interpreter);
result = thenTerm;
} else {
final Item elseTerm = this.elseTerm.evaluate(interpreter);
result = elseTerm;
}
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item ifTerm = this.ifTerm.substitute(interpreter);
final Item thenTerm = this.thenTerm.substitute(interpreter);
final Item elseTerm = this.elseTerm.substitute(interpreter);
return new ConditionalOperator(ifTerm, thenTerm, elseTerm);
}
@Override
public int typeOrder() {
return 20;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof ConditionalOperator) {
return compareTo((ConditionalOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(ConditionalOperator that) {
int order = this.ifTerm.compareTo(that.ifTerm);
if (order == 0) {
order = this.thenTerm.compareTo(that.thenTerm);
if (order == 0) {
order = this.elseTerm.compareTo(that.elseTerm);
}
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof ConditionalOperator) {
final ConditionalOperator that = (ConditionalOperator) other;
return this.ifTerm.equals(that.ifTerm) && this.thenTerm.equals(that.thenTerm)
&& this.elseTerm.equals(that.elseTerm);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(ConditionalOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(Murmur3.mix(hashSeed,
this.ifTerm.hashCode()), this.thenTerm.hashCode()), this.elseTerm.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.ifTerm).write('.').write("conditional").write('(')
.debug(this.thenTerm).write(", ").debug(this.elseTerm).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/DivideOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a division operation between its
* two operands.
*/
public final class DivideOperator extends BinaryOperator {
public DivideOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "/";
}
@Override
public int precedence() {
return 9;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.divide(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.divide(argument2);
}
@Override
public int typeOrder() {
return 35;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof DivideOperator) {
return compareTo((DivideOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(DivideOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof DivideOperator) {
final DivideOperator that = (DivideOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(DivideOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("divide").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/EqOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents an equality comparison between its
* two operands.
*/
public final class EqOperator extends BinaryOperator {
public EqOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "==";
}
@Override
public int precedence() {
return 0;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.eq(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.eq(argument2);
}
@Override
public int typeOrder() {
return 28;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof EqOperator) {
return compareTo((EqOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(EqOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof EqOperator) {
final EqOperator that = (EqOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(EqOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("eq").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/GeOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents greater than or equal to operation
* between its two operands.
*/
public final class GeOperator extends BinaryOperator {
public GeOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return ">=";
}
@Override
public int precedence() {
return 0;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.ge(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.ge(argument2);
}
@Override
public int typeOrder() {
return 30;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof GeOperator) {
return compareTo((GeOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(GeOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof GeOperator) {
final GeOperator that = (GeOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(GeOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("ge").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/GtOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a strictly greater than operation
* between its two operands.
*/
public final class GtOperator extends BinaryOperator {
public GtOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return ">";
}
@Override
public int precedence() {
return 0;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.gt(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.gt(argument2);
}
@Override
public int typeOrder() {
return 31;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof GtOperator) {
return compareTo((GtOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(GtOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof GtOperator) {
final GtOperator that = (GtOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(GtOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("gt").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/InvokeOperator.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.operator;
import swim.codec.Output;
import swim.structure.Func;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.structure.Value;
import swim.util.Murmur3;
public final class InvokeOperator extends Operator {
final Value func;
final Value args;
Object state;
public InvokeOperator(Value func, Value args) {
this.func = func;
this.args = args;
}
public Value func() {
return this.func;
}
public Value args() {
return this.args;
}
public Object state() {
return this.state;
}
public void setState(Object state) {
this.state = state;
}
@Override
public boolean isConstant() {
return this.func.isConstant() && this.args.isConstant();
}
@Override
public int precedence() {
return 11;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item result;
final Item func = this.func.evaluate(interpreter);
if (func instanceof Func) {
result = ((Func) func).invoke(this.args, interpreter, this);
} else {
result = Item.absent();
}
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item func = this.func.evaluate(interpreter);
if (func instanceof Func) {
final Item result = ((Func) func).expand(this.args, interpreter, this);
if (result != null) {
return result;
}
}
final Value args = this.args.substitute(interpreter).toValue();
return new InvokeOperator(this.func, args);
}
@Override
public int typeOrder() {
return 41;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof InvokeOperator) {
return compareTo((InvokeOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(InvokeOperator that) {
int order = this.func.compareTo(that.func);
if (order == 0) {
order = this.args.compareTo(that.args);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof InvokeOperator) {
final InvokeOperator that = (InvokeOperator) other;
return this.func.equals(that.func) && this.args.equals(that.args);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(InvokeOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.func.hashCode()), this.args.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.func).write('.').write("invoke").write('(').debug(this.args).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/LeOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a less than or equal to comparison
* between its two operands.
*/
public class LeOperator extends BinaryOperator {
public LeOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "<=";
}
@Override
public int precedence() {
return 0;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.le(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.le(argument2);
}
@Override
public int typeOrder() {
return 27;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof LeOperator) {
return compareTo((LeOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(LeOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof LeOperator) {
final LeOperator that = (LeOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(LeOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("le").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/LtOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a strictly less than comparison
* between its two operands.
*/
public class LtOperator extends BinaryOperator {
public LtOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "<";
}
@Override
public int precedence() {
return 0;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.lt(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.lt(argument2);
}
@Override
public int typeOrder() {
return 26;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof LtOperator) {
return compareTo((LtOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(LtOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof LtOperator) {
final LtOperator that = (LtOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(LtOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("lt").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/MinusOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a subtraction operation between its
* two operands.
*/
public final class MinusOperator extends BinaryOperator {
public MinusOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "-";
}
@Override
public int precedence() {
return 8;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.minus(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.minus(argument2);
}
@Override
public int typeOrder() {
return 33;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof MinusOperator) {
return compareTo((MinusOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(MinusOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof MinusOperator) {
final MinusOperator that = (MinusOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(MinusOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("minus").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/ModuloOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a modulus operation between its two
* operands.
*/
public final class ModuloOperator extends BinaryOperator {
public ModuloOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "%";
}
@Override
public int precedence() {
return 9;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.modulo(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.modulo(argument2);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof ModuloOperator) {
final ModuloOperator that = (ModuloOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int typeOrder() {
return 36;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof ModuloOperator) {
return compareTo((ModuloOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(ModuloOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(ModuloOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("modulo").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/NeOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents an inequality comparison between
* its two operands.
*/
public final class NeOperator extends BinaryOperator {
public NeOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "!=";
}
@Override
public int precedence() {
return 0;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.ne(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public int typeOrder() {
return 29;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof NeOperator) {
return compareTo((NeOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(NeOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof NeOperator) {
final NeOperator that = (NeOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(NeOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("ne").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/NegativeOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class NegativeOperator extends UnaryOperator {
public NegativeOperator(Item operand) {
super(operand);
}
@Override
public String operator() {
return "-";
}
@Override
public int precedence() {
return 10;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument = this.operand.evaluate(interpreter);
final Item result = argument.negative();
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument = this.operand.substitute(interpreter);
return argument.negative();
}
@Override
public int typeOrder() {
return 39;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof NegativeOperator) {
return compareTo((NegativeOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(NegativeOperator that) {
return this.operand.compareTo(that.operand);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof NegativeOperator) {
final NegativeOperator that = (NegativeOperator) other;
return this.operand.equals(that.operand);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(NegativeOperator.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.operand.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand).write('.').write("negative").write('(').write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/NotOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class NotOperator extends UnaryOperator {
public NotOperator(Item operand) {
super(operand);
}
@Override
public String operator() {
return "!";
}
@Override
public int precedence() {
return 10;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument = this.operand.evaluate(interpreter);
final Item result = argument.not();
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument = this.operand.substitute(interpreter);
return argument.not();
}
@Override
public int typeOrder() {
return 37;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof NotOperator) {
return compareTo((NotOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(NotOperator that) {
return this.operand.compareTo(that.operand);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof NotOperator) {
final NotOperator that = (NotOperator) other;
return this.operand.equals(that.operand);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(NotOperator.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.operand.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand).write('.').write("not").write('(').write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/OrOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class OrOperator extends BinaryOperator {
public OrOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "||";
}
@Override
public int precedence() {
return 3;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item result;
final Item argument1 = this.operand1.evaluate(interpreter);
if (argument1.booleanValue(false)) {
result = argument1;
} else {
final Item argument2 = this.operand2.evaluate(interpreter);
result = argument2;
}
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.or(argument2);
}
@Override
public int typeOrder() {
return 21;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof OrOperator) {
return compareTo((OrOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(OrOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof OrOperator) {
final OrOperator that = (OrOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(OrOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("or").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/PlusOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents an addition operation between its
* two operands.
*/
public final class PlusOperator extends BinaryOperator {
public PlusOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "+";
}
@Override
public int precedence() {
return 8;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.plus(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.plus(argument2);
}
@Override
public int typeOrder() {
return 32;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof PlusOperator) {
return compareTo((PlusOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(PlusOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof PlusOperator) {
final PlusOperator that = (PlusOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(PlusOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("plus").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/PositiveOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
public final class PositiveOperator extends UnaryOperator {
public PositiveOperator(Item operand) {
super(operand);
}
@Override
public String operator() {
return "+";
}
@Override
public int precedence() {
return 10;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument = this.operand.evaluate(interpreter);
final Item result = argument.positive();
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument = this.operand.substitute(interpreter);
return argument.positive();
}
@Override
public int typeOrder() {
return 40;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof PositiveOperator) {
return compareTo((PositiveOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(PositiveOperator that) {
return this.operand.compareTo(that.operand);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof PositiveOperator) {
final PositiveOperator that = (PositiveOperator) other;
return this.operand.equals(that.operand);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(PositiveOperator.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.operand.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand).write('.').write("positive").write('(').write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/TimesOperator.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.operator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Operator;
import swim.util.Murmur3;
/**
* A {@link BinaryOperator} that represents a multiplication operation between
* its two operands.
*/
public final class TimesOperator extends BinaryOperator {
public TimesOperator(Item operand1, Item operand2) {
super(operand1, operand2);
}
@Override
public String operator() {
return "*";
}
@Override
public int precedence() {
return 9;
}
@Override
public Item evaluate(Interpreter interpreter) {
interpreter.willOperate(this);
final Item argument1 = this.operand1.evaluate(interpreter);
final Item argument2 = this.operand2.evaluate(interpreter);
final Item result = argument1.times(argument2);
interpreter.didOperate(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item argument1 = this.operand1.substitute(interpreter);
final Item argument2 = this.operand2.substitute(interpreter);
return argument1.times(argument2);
}
@Override
public int typeOrder() {
return 34;
}
@Override
protected int compareTo(Operator that) {
if (that instanceof TimesOperator) {
return compareTo((TimesOperator) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(TimesOperator that) {
int order = this.operand1.compareTo(that.operand1);
if (order == 0) {
order = this.operand2.compareTo(that.operand2);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof TimesOperator) {
final TimesOperator that = (TimesOperator) other;
return this.operand1.equals(that.operand1) && this.operand2.equals(that.operand2);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(TimesOperator.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.operand1.hashCode()), this.operand2.hashCode()));
}
@Override
public void debug(Output<?> output) {
output.debug(this.operand1).write('.').write("times").write('(').debug(this.operand2).write(')');
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/UnaryOperator.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.operator;
import swim.structure.Item;
import swim.structure.Operator;
/**
* An {@link Operator} that represents a unary operation, i.e. an operation on
* one operand.
*/
public abstract class UnaryOperator extends Operator {
final Item operand;
public UnaryOperator(Item operand) {
this.operand = operand.commit();
}
public final Item operand() {
return this.operand;
}
/**
* Returns the token that identifiers this {@code Operator}'s operation.
* Used to uniquely identify the type of operation, and to aid serialization.
*/
public abstract String operator();
@Override
public boolean isConstant() {
return this.operand.isConstant();
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/operator/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.
/**
* Operator expression trees.
*/
package swim.structure.operator;
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/ChildrenSelector.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.selector;
import java.util.ListIterator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Value;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated} against some {@link
* Interpreter}, yields all of the "children" of the top {@code Item} in the
* interpreter's frame stack. A "child" is defined to be a top-level {@link
* Item} in a {@link Record}; it has no definition for any other type.
*/
public class ChildrenSelector extends Selector {
final Selector then;
ChildrenSelector(Selector then) {
this.then = then;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
// Only records can have children.
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
// For each child, while none have been selected:
while (selected == null && children.hasNext()) {
final Item child = children.next();
// Push the child onto the scope stack.
interpreter.pushScope(child);
// Subselect the child.
selected = this.then.forSelected(interpreter, callback);
// Pop the child off of the scope stack.
interpreter.popScope();
}
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
// Only records can have children.
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
// For each child:
while (children.hasNext()) {
final Item oldChild = children.next();
// Push the child onto the scope stack.
interpreter.pushScope(oldChild);
// Transform the child.
final Item newChild = this.then.mapSelected(interpreter, transform);
// Pop the child off the scope stack.
interpreter.popScope();
if (newChild.isDefined()) {
// Update the child, if its identity changed.
if (newChild != oldChild) {
children.set(newChild);
}
} else {
// Remove the child, if it transformed to Absent.
children.remove();
}
}
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new ChildrenSelector((Selector) then);
}
@Override
public Selector andThen(Selector then) {
return new ChildrenSelector(this.then.andThen(then));
}
@Override
public int typeOrder() {
return 17;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof ChildrenSelector) {
return compareTo((ChildrenSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(ChildrenSelector that) {
return this.then.compareTo(that.then);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof ChildrenSelector) {
final ChildrenSelector that = (ChildrenSelector) other;
return this.then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(ChildrenSelector.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("children").write('(').write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/DescendantsSelector.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.selector;
import java.util.ListIterator;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Value;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated} against some {@link
* Interpreter} {@code stack}, yields all of the "descendants" of the top {@code
* Item} in {@code stack}. A "descendant" is recursively defined to be a {@link
* Record Record's} {@link ChildrenSelector children} and their descendants;
* it has no definition for any other type.
*/
public class DescendantsSelector extends Selector {
final Selector then;
DescendantsSelector(Selector then) {
this.then = then;
}
@Override
public Selector then() {
return this.then;
}
/**
* Invokes {@code then.forSelected()} against the {@code Interpreter}
* containing each child, then moves onto each descendant.
*/
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
// Only records can have descendants.
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
// For each child, while none have selected a result:
while (selected == null && children.hasNext()) {
final Item child = children.next();
// Push the child onto the scope stack.
interpreter.pushScope(child);
// Subselect the child.
selected = this.then.forSelected(interpreter, callback);
// If the child was not selected:
if (selected == null) {
// Recursively select the child's children.
forSelected(interpreter, callback);
}
// Pop the child off of the scope stack.
interpreter.popScope();
}
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
// Only records can have descendants.
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
// For each child:
while (children.hasNext()) {
final Item oldChild = children.next();
// Push the child onto the scope stack.
interpreter.pushScope(oldChild);
// Transform the child.
Item newChild = this.then.mapSelected(interpreter, transform);
// If the child was not removed:
if (newChild.isDefined()) {
// Recursively transform the child's children.
newChild = mapSelected(interpreter, transform);
}
// Pop the child off the scope stack.
interpreter.popScope();
if (newChild.isDefined()) {
// Update the child, if its identity changed.
if (newChild != oldChild) {
children.set(newChild);
}
} else {
// Remove the child, if it transformed to Absent.
children.remove();
}
}
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new DescendantsSelector((Selector) then);
}
@Override
public Selector andThen(Selector then) {
return new DescendantsSelector(this.then.andThen(then));
}
@Override
public int typeOrder() {
return 18;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof DescendantsSelector) {
return compareTo((DescendantsSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(DescendantsSelector that) {
return this.then.compareTo(that.then);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof DescendantsSelector) {
final DescendantsSelector that = (DescendantsSelector) other;
return this.then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(DescendantsSelector.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("descendants").write('(').write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/FilterSelector.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.selector;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated}, yields each {@code
* Item} in {@code interpreter} such that {@code evaluating} {@code predicate}
* against this {@code Item} would select at least one defined result. This is
* fundamentally different from returning the result itself; for
* example,
* <p>
* {@code FilterSelector(predicate=GetSelector(key="a")).evaluate(Record(Slot("a",5))}
* <p>
* yields the {@code Record} itself, NOT just {@code 5}.
* <p>
* To accomplish this, {@code FilterSelector} itself implements {@link Selectee
* Selectee<Item>.selected} by always returning {@link
* swim.structure.Extant} which, crucially, is never null. {@link #forSelected
* forSelected} still takes the form "if (condition) then subselect"; here,
* "condition" is true only if {@code predicate.forSelected(interpreter,this)}
* is not null. Thus, the responsibility to ensure that {@code this.selected}
* is invoked only if {@code predicate} would select something in {@code
* Interpreter} is in {@code predicate.forSelected}--exactly as it should be.
*/
public final class FilterSelector extends Selector implements Selectee<Item> {
final Selector predicate;
final Selector then;
public FilterSelector(Selector predicate, Selector then) {
this.predicate = predicate;
this.then = then;
}
public Selector predicate() {
return this.predicate;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
// If the filter matches the selection scope:
if (filterSelected(interpreter)) {
// Then subselect the selection scope.
selected = this.then.forSelected(interpreter, callback);
}
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
// If the filter matches the selection scope:
if (filterSelected(interpreter)) {
// Then transform the selection scope.
result = this.then.mapSelected(interpreter, transform);
} else {
result = interpreter.peekScope().toValue();
}
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
Item predicate = this.predicate.substitute(interpreter);
if (!(predicate instanceof Selector)) {
predicate = this.predicate;
}
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new FilterSelector((Selector) predicate, (Selector) then);
}
protected boolean filterSelected(Interpreter interpreter) {
return this.predicate.forSelected(interpreter, this) != null;
}
/**
* Always returns {@link swim.structure.Extant}, which, crucially, is never
* null. See {@link FilterSelector} for an explanation.
*/
@Override
public Item selected(Interpreter interpreter) {
return Item.extant();
}
@Override
public Selector andThen(Selector then) {
return new FilterSelector(this.predicate, this.then.andThen(then));
}
@Override
public FilterSelector filter() {
return this;
}
@Override
public int typeOrder() {
return 19;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof FilterSelector) {
return compareTo((FilterSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(FilterSelector that) {
int order = this.predicate.compareTo(that.predicate);
if (order == 0) {
order = this.then.compareTo(that.then);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof FilterSelector) {
final FilterSelector that = (FilterSelector) other;
return this.predicate.equals(that.predicate) && this.then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(FilterSelector.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.predicate.hashCode()), this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("filter").write('(').debug(this.predicate).write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/GetAttrSelector.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.selector;
import swim.codec.Output;
import swim.structure.Attr;
import swim.structure.Field;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Text;
import swim.structure.Value;
import swim.util.Murmur3;
public final class GetAttrSelector extends Selector {
final Text key;
final Selector then;
public GetAttrSelector(Text key, Selector then) {
this.key = key;
this.then = then;
}
public Text accessor() {
return this.key;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
interpreter.willSelect(this);
final Value key = this.key;
final T selected = forSelected(key, this.then, interpreter, callback);
interpreter.didSelect(this, selected);
return selected;
}
private static <T> T forSelected(Value key, Selector then, Interpreter interpreter, Selectee<T> callback) {
T selected = null;
if (interpreter.scopeDepth() != 0) {
// Pop the next selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
final Field field;
// Only records can have members.
if (scope instanceof Record) {
field = scope.getField(key);
if (field instanceof Attr) {
// Push the field value onto the scope stack.
interpreter.pushScope(field.toValue());
// Subselect the field value.
selected = then.forSelected(interpreter, callback);
// Pop the field value off of the scope stack.
interpreter.popScope();
}
} else {
field = null;
}
if (field == null && selected == null) {
forSelected(key, then, interpreter, callback);
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
final Text key = this.key;
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
// Only records can have members.
if (scope instanceof Record) {
final Record record = (Record) scope;
final Field oldField = record.getField(key);
if (oldField instanceof Attr) {
// Push the field value onto the scope stack.
interpreter.pushScope(oldField.toValue());
// Transform the field value.
final Item newItem = this.then.mapSelected(interpreter, transform);
// Pop the field value off the scope stack.
interpreter.popScope();
if (newItem instanceof Field) {
// Replace the original field with the transformed field.
if (key.equals(newItem.key())) {
record.putAttr(key, newItem.toValue());
} else {
record.remove(key);
record.add(newItem);
}
} else if (newItem.isDefined()) {
// Update the field with the transformed value.
record.putAttr(key, newItem.toValue());
} else {
// Remove the field.
record.remove(key);
}
}
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Text key = this.key;
final Item value = substitute(key, this.then, interpreter);
if (value != null) {
return value;
}
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new GetAttrSelector(this.key, (Selector) then);
}
private static Item substitute(Text key, Selector then, Interpreter interpreter) {
Item selected = null;
if (interpreter.scopeDepth() != 0) {
// Pop the next selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
final Field field;
// Only records can have members.
if (scope instanceof Record) {
field = scope.getField(key);
if (field instanceof Attr) {
// Substitute the field value.
selected = field.toValue().substitute(interpreter);
}
} else {
field = null;
}
if (field != null && selected != null) {
substitute(key, then, interpreter);
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
return selected;
}
@Override
public Selector andThen(Selector then) {
return new GetAttrSelector(this.key, this.then.andThen(then));
}
@Override
public int typeOrder() {
return 13;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof GetAttrSelector) {
return compareTo((GetAttrSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(GetAttrSelector that) {
int order = this.key.compareTo(that.key);
if (order == 0) {
order = this.then.compareTo(that.then);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof GetAttrSelector) {
final GetAttrSelector that = (GetAttrSelector) other;
return key.equals(that.key) && then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(GetAttrSelector.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.key.hashCode()), this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("getAttr").write('(').debug(this.key).write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/GetItemSelector.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.selector;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Value;
import swim.util.Murmur3;
public final class GetItemSelector extends Selector {
final Num index;
final Selector then;
public GetItemSelector(Num index, Selector then) {
this.index = index;
this.then = then;
}
public Num accessor() {
return this.index;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
final int index = this.index.intValue();
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
if (scope instanceof Record && index < scope.length()) {
final Item item = scope.getItem(index);
// Push the item onto the scope stack.
interpreter.pushScope(item);
// Subselect the item.
selected = this.then.forSelected(interpreter, callback);
// Pop the item off of the scope stack.
interpreter.popScope();
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
final int index = this.index.intValue();
if (scope instanceof Record && index < scope.length()) {
final Item oldItem = scope.getItem(index);
// Push the item onto the scope stack.
interpreter.pushScope(oldItem);
// Transform the item.
final Item newItem = this.then.mapSelected(interpreter, transform);
// Pop the item off the scope stack.
interpreter.popScope();
if (newItem.isDefined()) {
((Record) scope).setItem(index, newItem);
} else {
((Record) scope).remove(index);
}
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final int index = this.index.intValue();
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
final Item selected;
if (scope instanceof Record && index < scope.length()) {
final Item item = scope.getItem(index);
// Substitute the item.
selected = item.substitute(interpreter);
} else {
selected = null;
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
if (selected != null) {
return selected;
}
}
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new GetItemSelector(this.index, (Selector) then);
}
@Override
public Selector andThen(Selector then) {
return new GetItemSelector(index, this.then.andThen(then));
}
@Override
public int typeOrder() {
return 14;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof GetItemSelector) {
return compareTo((GetItemSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(GetItemSelector that) {
int order = this.index.compareTo(that.index);
if (order == 0) {
order = this.then.compareTo(that.then);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof GetItemSelector) {
final GetItemSelector that = (GetItemSelector) other;
return index.equals(that.index) && then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(GetItemSelector.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.index.hashCode()), this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("getItem").write('(').debug(this.index).write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/GetSelector.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.selector;
import swim.codec.Output;
import swim.structure.Expression;
import swim.structure.Field;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Value;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated}, searches all
* variables in its evaluation scope and yields the most recent {@link Value}
* that corresponds to {@code key}. Note that {@code key} itself can be an
* {@link Expression}, in which case it will be {@code evaluated} against {@code
* stack} prior to any concrete selection logic.
*/
public final class GetSelector extends Selector {
final Value key;
final Selector then;
public GetSelector(Value key, Selector then) {
this.key = key.commit();
this.then = then;
}
public Value accessor() {
return this.key;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
interpreter.willSelect(this);
// Evaluate the key, in case it's dynamic.
final Value key = this.key.evaluate(interpreter).toValue();
final T selected = forSelected(key, this.then, interpreter, callback);
interpreter.didSelect(this, selected);
return selected;
}
private static <T> T forSelected(Value key, Selector then, Interpreter interpreter, Selectee<T> callback) {
T selected = null;
if (interpreter.scopeDepth() != 0) {
// Pop the next selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
final Field field;
// Only records can have members.
if (scope instanceof Record) {
field = scope.getField(key);
if (field != null) {
// Push the field value onto the scope stack.
interpreter.pushScope(field.toValue());
// Subselect the field value.
selected = then.forSelected(interpreter, callback);
// Pop the field value off of the scope stack.
interpreter.popScope();
}
} else {
field = null;
}
if (field == null && selected == null) {
forSelected(key, then, interpreter, callback);
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
// Evaluate the key, if it's dynamic.
final Value key = this.key.evaluate(interpreter).toValue();
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
// Only records can have members.
if (scope instanceof Record) {
final Record record = (Record) scope;
final Field oldField = record.getField(key);
if (oldField != null) {
// Push the field value onto the scope stack.
interpreter.pushScope(oldField.toValue());
// Transform the field value.
final Item newItem = this.then.mapSelected(interpreter, transform);
// Pop the field value off the scope stack.
interpreter.popScope();
if (newItem instanceof Field) {
// Replace the original field with the transformed field.
if (key.equals(newItem.key())) {
record.put(key, newItem.toValue());
} else {
record.remove(key);
record.add(newItem);
}
} else if (newItem.isDefined()) {
// Update the field with the transformed value.
record.put(key, newItem.toValue());
} else {
// Remove the field.
record.remove(key);
}
}
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
// Evaluate the key, in case it's dynamic.
final Value key = this.key.evaluate(interpreter).toValue();
final Item value = substitute(key, this.then, interpreter);
if (value != null) {
return value;
}
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new GetSelector(this.key, (Selector) then);
}
private static Item substitute(Value key, Selector then, Interpreter interpreter) {
Item selected = null;
if (interpreter.scopeDepth() != 0) {
// Pop the next selection off of the stack to take it out of scope.
final Value scope = interpreter.popScope().toValue();
final Field field;
// Only records can have members.
if (scope instanceof Record) {
field = scope.getField(key);
if (field != null) {
// Substitute the field value.
selected = field.toValue().substitute(interpreter);
}
} else {
field = null;
}
if (field != null && selected != null) {
substitute(key, then, interpreter);
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
return selected;
}
@Override
public Selector andThen(Selector then) {
return new GetSelector(this.key, this.then.andThen(then));
}
@Override
public int typeOrder() {
return 12;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof GetSelector) {
return compareTo((GetSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(GetSelector that) {
int order = this.key.compareTo(that.key);
if (order == 0) {
order = this.then.compareTo(that.then);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof GetSelector) {
final GetSelector that = (GetSelector) other;
return key.equals(that.key) && then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(GetSelector.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.key.hashCode()), this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("get").write('(').debug(this.key).write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/IdentitySelector.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.selector;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Num;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Text;
import swim.structure.Value;
import swim.util.Murmur3;
public final class IdentitySelector extends Selector {
@Override
public Selector then() {
return this;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Item oldScope = interpreter.popScope();
// Evaluate the current selection.
final Item newScope = oldScope.evaluate(interpreter);
// Push the evaluated selection onto the scope stack.
interpreter.pushScope(newScope);
// Visit the evaluated selection.
selected = callback.selected(interpreter);
// Restore the original selection to the top of the scope stack.
interpreter.swapScope(oldScope);
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
return transform.selected(interpreter);
}
@Override
public Item substitute(Interpreter interpreter) {
return interpreter.peekScope().substitute(interpreter);
}
@Override
public Selector get(Value key) {
return new GetSelector(key, this);
}
@Override
public Selector getAttr(Text key) {
return new GetAttrSelector(key, this);
}
@Override
public Selector getItem(Num index) {
return new GetItemSelector(index, this);
}
@Override
public Selector andThen(Selector then) {
return then;
}
@Override
public Selector keys() {
if (keys == null) {
keys = new KeysSelector(this);
}
return keys;
}
@Override
public Selector values() {
if (values == null) {
values = new ValuesSelector(this);
}
return values;
}
@Override
public Selector children() {
if (children == null) {
children = new ChildrenSelector(this);
}
return children;
}
@Override
public Selector descendants() {
if (descendants == null) {
descendants = new DescendantsSelector(this);
}
return descendants;
}
@Override
public int typeOrder() {
return 10;
}
@Override
protected int compareTo(Selector that) {
return Integer.compare(typeOrder(), that.typeOrder());
}
@Override
public boolean equals(Object other) {
return this == other;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(IdentitySelector.class);
}
return hashSeed;
}
@Override
public void debugThen(Output<?> output) {
// nop
}
private static final IdentitySelector IDENTITY = new IdentitySelector();
private static Selector keys;
private static Selector values;
private static Selector children;
private static Selector descendants;
private static int hashSeed;
public static IdentitySelector identity() {
return IDENTITY;
}
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/KeysSelector.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.selector;
import java.util.ListIterator;
import swim.codec.Output;
import swim.structure.Attr;
import swim.structure.Field;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Slot;
import swim.structure.Text;
import swim.structure.Value;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated} against some {@link
* Interpreter}, yields all of the "keys" of the top {@code Item} in the
* interpreter's frame stack. The "keys" of an {@code Item} {@code item} are
* defined to be either the sole {@link Field ((Field) item).key} if {@code
* item} is a {@code Field}, or every such key of every {@code Field} in {@code
* item} if {@code item} is a {@code Record}; "keys" are not defined for any
* other type.
*/
public final class KeysSelector extends Selector {
final Selector then;
public KeysSelector(Selector then) {
this.then = then;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Item scope = interpreter.popScope();
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
// For each child, while none have been selected:
while (selected == null && children.hasNext()) {
final Item child = children.next();
// Only fields can have keys.
if (child instanceof Field) {
// Push the child key onto the scope stack.
interpreter.pushScope(child.key());
// Subselect the child key.
selected = this.then.forSelected(interpreter, callback);
// Pop the child key off of the scope stack.
interpreter.popScope();
}
}
} else if (scope instanceof Field) {
// Push the key onto the scope stack.
interpreter.pushScope(scope.key());
// Subselect the key.
selected = this.then.forSelected(interpreter, callback);
// Pop the key off of the scope stack.
interpreter.popScope();
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
Item scope = interpreter.popScope();
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
while (children.hasNext()) {
final Item child = children.next();
if (child instanceof Field) {
final Value oldKey = child.key();
// Push the key onto the scope stack.
interpreter.pushScope(oldKey);
// Subselect the key.
final Value newKey = this.then.mapSelected(interpreter, transform).toValue();
// Pop the key off of the scope stack.
interpreter.popScope();
if (newKey.isDefined()) {
if (oldKey != newKey) {
if (scope instanceof Attr && newKey instanceof Text) {
children.set(Attr.of((Text) newKey, scope.toValue()));
} else {
children.set(Slot.of(newKey, scope.toValue()));
}
}
} else {
children.remove();
}
}
}
} else if (scope instanceof Field) {
final Value oldKey = scope.key();
// Push the key onto the scope stack.
interpreter.pushScope(oldKey);
// Subselect the key.
final Value newKey = this.then.mapSelected(interpreter, transform).toValue();
// Pop the key off of the scope stack.
interpreter.popScope();
if (newKey.isDefined()) {
if (oldKey != newKey) {
if (scope instanceof Attr && newKey instanceof Text) {
scope = Attr.of((Text) newKey, scope.toValue());
} else {
scope = Slot.of(newKey, scope.toValue());
}
}
} else {
scope = Item.absent();
}
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new KeysSelector((Selector) then);
}
@Override
public Selector andThen(Selector then) {
return new KeysSelector(this.then.andThen(then));
}
@Override
public int typeOrder() {
return 15;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof KeysSelector) {
return compareTo((KeysSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(KeysSelector that) {
return this.then.compareTo(that.then);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof KeysSelector) {
final KeysSelector that = (KeysSelector) other;
return then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(KeysSelector.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("keys").write('(').write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/LiteralSelector.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.selector;
import swim.codec.Output;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated}, evaluates and
* yields {@code item} against {@code interpreter}. This allows us to express
* various selection criteria without having to implement the corresponding
* dedicated {@code Selectors}.
* <p>
* Such behavior is <i>especially</i> useful in conjunction with {@link
* FilterSelector FilterSelectors}. For example, to select {@code Items} in some
* {@code Interpreter} such that the sum of the results of {@code selecting} "a"
* and "b" is less than 5, we could do
* <p>
* {@code Selector.literal(Selector.get("a").plus(Selector.get("b")).lt(5))}
* <p>
* This, notably, does not require a "ConditionalSelector" and only needs
* correct implementations of {@link swim.structure.operator.LtOperator} and
* {@link swim.structure.operator.PlusOperator}.
*/
public final class LiteralSelector extends Selector {
final Item item;
final Selector then;
public LiteralSelector(Item item, Selector then) {
this.item = item.commit();
this.then = then;
}
public Item item() {
return this.item;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
final Item literal = this.item.evaluate(interpreter);
if (literal.isDefined()) {
// Push the literal onto the scope stack.
interpreter.pushScope(literal);
// Subselect the literal.
selected = this.then.forSelected(interpreter, callback);
// Pop the literal off of the scope stack.
interpreter.popScope();
}
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
Item literal = this.item.evaluate(interpreter);
if (literal.isDefined()) {
// Push the literal onto the scope stack.
interpreter.pushScope(literal);
// Transform the literal.
literal = this.then.mapSelected(interpreter, transform);
// Pop the literal off of the scope stack.
interpreter.popScope();
}
result = literal;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
final Item item = this.item.substitute(interpreter);
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new LiteralSelector(item, (Selector) then);
}
@Override
public Selector andThen(Selector that) {
return new LiteralSelector(this.item, this.then.andThen(then));
}
@Override
public int precedence() {
return this.item.precedence();
}
@Override
public int typeOrder() {
return 11;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof LiteralSelector) {
return compareTo((LiteralSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(LiteralSelector that) {
int order = this.item.compareTo(that.item);
if (order == 0) {
order = this.then.compareTo(that.then);
}
return order;
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof LiteralSelector) {
final LiteralSelector that = (LiteralSelector) other;
return this.item.equals(that.item) && this.then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(LiteralSelector.class);
}
return Murmur3.mash(Murmur3.mix(Murmur3.mix(hashSeed,
this.item.hashCode()), this.then.hashCode()));
}
@Override
public void debug(Output<?> output) {
output = output.write("Selector").write('.').write("literal").write('(').debug(this.item).write(')');
this.then.debugThen(output);
}
@Override
public void debugThen(Output<?> output) {
// nop
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/ValuesSelector.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.selector;
import java.util.ListIterator;
import swim.codec.Output;
import swim.structure.Field;
import swim.structure.Interpreter;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Selectee;
import swim.structure.Selector;
import swim.structure.Value;
import swim.util.Murmur3;
/**
* A {@link Selector} that, when {@link #evaluate evaluated} against some {@link
* Interpreter}, yields all of the "values" of the top {@code Item} in the
* interpreter's frame stack. Every {@code Item} that is not a {@code Record}
* has exactly one "value" whose definition is consistent with {@link
* Item#toValue}. The "values" of a {@code Record} are defined as the set of
* every such "value" for every (top-level) {@code Item} in the {@code Record}.
*/
public class ValuesSelector extends Selector {
final Selector then;
public ValuesSelector(Selector then) {
this.then = then;
}
@Override
public Selector then() {
return this.then;
}
@Override
public <T> T forSelected(Interpreter interpreter, Selectee<T> callback) {
T selected = null;
interpreter.willSelect(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
final Item scope = interpreter.popScope();
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
// For each child, while none have been selected:
while (selected == null && children.hasNext()) {
final Item child = children.next();
// Push the child value onto the scope stack.
interpreter.pushScope(child.toValue());
// Subselect the child value.
selected = this.then.forSelected(interpreter, callback);
// Pop the child value off of the scope stack.
interpreter.popScope();
}
} else {
// Push the value onto the scope stack.
interpreter.pushScope(scope.toValue());
// Subselect the value.
selected = this.then.forSelected(interpreter, callback);
// Pop the value off of the scope stack.
interpreter.popScope();
}
// Push the current selection back onto the stack.
interpreter.pushScope(scope);
}
interpreter.didSelect(this, selected);
return selected;
}
@Override
public Item mapSelected(Interpreter interpreter, Selectee<Item> transform) {
final Item result;
interpreter.willTransform(this);
if (interpreter.scopeDepth() != 0) {
// Pop the current selection off of the stack to take it out of scope.
Item scope = interpreter.popScope();
if (scope instanceof Record) {
final ListIterator<Item> children = ((Record) scope).listIterator();
while (children.hasNext()) {
final Item child = children.next();
if (child instanceof Field) {
final Value oldValue = child.toValue();
// Push the child value onto the scope stack.
interpreter.pushScope(oldValue);
// Transform the child value.
final Item newItem = this.then.mapSelected(interpreter, transform);
// Pop the child value off of the scope stack.
interpreter.popScope();
if (newItem.isDefined()) {
if (newItem instanceof Field) {
children.set(newItem);
} else if (newItem != oldValue) {
children.set(((Field) child).updatedValue(newItem.toValue()));
}
} else {
children.remove();
}
} else {
// Push the child onto the scope stack.
interpreter.pushScope(child.toValue());
// Transform the child.
final Item newItem = this.then.mapSelected(interpreter, transform);
// Pop the child off of the scope stack.
interpreter.popScope();
if (newItem.isDefined()) {
if (child != newItem) {
children.set(newItem);
}
} else {
children.remove();
}
}
}
} else if (scope instanceof Field) {
final Value oldValue = scope.toValue();
// Push the field value onto the scope stack.
interpreter.pushScope(oldValue);
// Transform the field value.
final Item newItem = this.then.mapSelected(interpreter, transform);
// Pop the field value off of the scope stack.
interpreter.popScope();
if (newItem.isDefined()) {
if (newItem instanceof Field) {
scope = newItem;
} else if (newItem != oldValue) {
scope = ((Field) scope).updatedValue(newItem.toValue());
}
} else {
scope = Item.absent();
}
} else {
// Push the value onto the scope stack.
interpreter.pushScope(scope);
// Transform the value.
scope = this.then.mapSelected(interpreter, transform);
// Pop the value off of the scope stack.
interpreter.popScope();
}
// Push the transformed selection back onto the stack.
interpreter.pushScope(scope);
result = scope;
} else {
result = Item.absent();
}
interpreter.didTransform(this, result);
return result;
}
@Override
public Item substitute(Interpreter interpreter) {
Item then = this.then.substitute(interpreter);
if (!(then instanceof Selector)) {
then = this.then;
}
return new ValuesSelector((Selector) then);
}
@Override
public Selector andThen(Selector then) {
return new ValuesSelector(this.then.andThen(then));
}
@Override
public int typeOrder() {
return 16;
}
@Override
protected int compareTo(Selector that) {
if (that instanceof ValuesSelector) {
return compareTo((ValuesSelector) that);
}
return Integer.compare(typeOrder(), that.typeOrder());
}
int compareTo(ValuesSelector that) {
return this.then.compareTo(that.then);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof ValuesSelector) {
final ValuesSelector that = (ValuesSelector) other;
return then.equals(that.then);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(ValuesSelector.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.then.hashCode()));
}
@Override
public void debugThen(Output<?> output) {
output = output.write('.').write("values").write('(').write(')');
this.then.debugThen(output);
}
private static int hashSeed;
}
|
0 | java-sources/ai/swim/swim-structure/3.10.0/swim/structure | java-sources/ai/swim/swim-structure/3.10.0/swim/structure/selector/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.
/**
* Selector expression trees.
*/
package swim.structure.selector;
|
0 | java-sources/ai/swim/swim-uri | java-sources/ai/swim/swim-uri/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.
/**
* Unfirom resource identifiers.
*/
module swim.uri {
requires swim.util;
requires transitive swim.codec;
requires transitive swim.collections;
requires transitive swim.structure;
exports swim.uri;
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/FlatteningIterator.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.uri;
import java.util.Iterator;
import java.util.NoSuchElementException;
abstract class FlatteningIterator<P, T> implements Iterator<T> {
Iterator<P> outer;
Iterator<T> inner;
FlatteningIterator(Iterator<P> outer) {
this.outer = outer;
}
protected abstract Iterator<T> childIterator(P parent);
public boolean hasNext() {
while (true) {
if (this.inner != null && this.inner.hasNext()) {
return true;
} else if (this.outer != null && this.outer.hasNext()) {
this.inner = childIterator(this.outer.next());
} else {
this.inner = null;
this.outer = null;
return false;
}
}
}
@Override
public T next() {
while (true) {
if (this.inner != null && this.inner.hasNext()) {
return this.inner.next();
} else if (this.outer != null && this.outer.hasNext()) {
this.inner = childIterator(this.outer.next());
} else {
this.inner = null;
this.outer = null;
throw new NoSuchElementException();
}
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/Uri.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.uri;
import java.util.Collection;
import java.util.Map;
import swim.codec.Base16;
import swim.codec.Debug;
import swim.codec.Display;
import swim.codec.Format;
import swim.codec.Output;
import swim.structure.Form;
import swim.structure.Kind;
import swim.util.Murmur3;
public class Uri implements Comparable<Uri>, Debug, Display {
protected final UriScheme scheme;
protected final UriAuthority authority;
protected final UriPath path;
protected final UriQuery query;
protected final UriFragment fragment;
String string;
protected Uri(UriScheme scheme, UriAuthority authority, UriPath path,
UriQuery query, UriFragment fragment) {
this.scheme = scheme;
this.authority = authority;
this.path = path;
this.query = query;
this.fragment = fragment;
}
public final boolean isDefined() {
return this.scheme.isDefined() || this.authority.isDefined() || this.path.isDefined()
|| this.query.isDefined() || this.fragment.isDefined();
}
public final boolean isEmpty() {
return !this.scheme.isDefined() && !this.authority.isDefined() && this.path.isEmpty()
&& !this.query.isDefined() && !this.fragment.isDefined();
}
public final UriScheme scheme() {
return this.scheme;
}
public Uri scheme(UriScheme scheme) {
if (scheme != this.scheme) {
return copy(scheme, this.authority, this.path, this.query, this.fragment);
} else {
return this;
}
}
public final String schemePart() {
return this.scheme.toString();
}
public Uri schemePart(String scheme) {
return scheme(UriScheme.parse(scheme));
}
public final String schemeName() {
return this.scheme.name();
}
public Uri schemeName(String scheme) {
return scheme(UriScheme.from(scheme));
}
public final UriAuthority authority() {
return this.authority;
}
public Uri authority(UriAuthority authority) {
if (authority != this.authority) {
return copy(this.scheme, authority, this.path, this.query, this.fragment);
} else {
return this;
}
}
public final String authorityPart() {
return this.authority.toString();
}
public Uri authorityPart(String authority) {
return authority(UriAuthority.parse(authority));
}
public final UriUser user() {
return this.authority.user();
}
public Uri user(UriUser user) {
return authority(this.authority.user(user));
}
public final String userPart() {
return this.authority.userPart();
}
public Uri userPart(String user) {
return authority(this.authority.userPart(user));
}
public String username() {
return this.authority.username();
}
public Uri username(String username) {
return authority(this.authority.username(username));
}
public Uri username(String username, String password) {
return authority(this.authority.username(username, password));
}
public String password() {
return this.authority.password();
}
public Uri password(String password) {
return authority(this.authority.password(password));
}
public final UriHost host() {
return this.authority.host();
}
public Uri host(UriHost host) {
return authority(this.authority.host(host));
}
public final String hostPart() {
return this.authority.hostPart();
}
public Uri hostPart(String host) {
return authority(this.authority.hostPart(host));
}
public final String hostAddress() {
return this.authority.hostAddress();
}
public final String hostName() {
return this.authority.hostName();
}
public Uri hostName(String address) {
return authority(this.authority.hostName(address));
}
public final String hostIPv4() {
return this.authority.hostIPv4();
}
public Uri hostIPv4(String address) {
return authority(this.authority.hostIPv4(address));
}
public final String hostIPv6() {
return this.authority.hostIPv6();
}
public Uri hostIPv6(String address) {
return authority(this.authority.hostIPv6(address));
}
public final UriPort port() {
return this.authority.port();
}
public Uri port(UriPort port) {
return authority(this.authority.port(port));
}
public final String portPart() {
return this.authority.portPart();
}
public Uri portPart(String port) {
return authority(this.authority.portPart(port));
}
public final int portNumber() {
return this.authority.portNumber();
}
public Uri portNumber(int number) {
return authority(this.authority.portNumber(number));
}
public final UriPath path() {
return this.path;
}
public Uri path(UriPath path) {
if (path != this.path) {
return copy(this.scheme, this.authority, path, this.query, this.fragment);
} else {
return this;
}
}
public Uri path(String... components) {
return path(UriPath.from(components));
}
public final String pathPart() {
return this.path.toString();
}
public Uri pathPart(String path) {
return path(UriPath.parse(path));
}
public final String pathName() {
return this.path.name();
}
public Uri pathName(String name) {
return path(this.path.name(name));
}
public final UriPath parentPath() {
return this.path.parent();
}
public final UriPath basePath() {
return this.path.base();
}
public final Uri parent() {
return Uri.from(this.scheme, this.authority, this.path.parent());
}
public final Uri base() {
return Uri.from(this.scheme, this.authority, this.path.base());
}
public Uri appendedPath(String component) {
return path(this.path.appended(component));
}
public Uri appendedPath(String... components) {
return path(this.path.appended(components));
}
public Uri appendedPath(Collection<? extends String> components) {
return path(this.path.appended(components));
}
public Uri appendedSlash() {
return path(this.path.appendedSlash());
}
public Uri appendedSegment(String segment) {
return path(this.path.appendedSegment(segment));
}
public Uri prependedPath(String component) {
return path(this.path.prepended(component));
}
public Uri prependedPath(String... components) {
return path(this.path.prepended(components));
}
public Uri prependedPath(Collection<? extends String> components) {
return path(this.path.prepended(components));
}
public Uri prependedSlash() {
return path(this.path.prependedSlash());
}
public Uri prependedSegment(String segment) {
return path(this.path.prependedSegment(segment));
}
public final UriQuery query() {
return this.query;
}
public Uri query(UriQuery query) {
if (query != this.query) {
return copy(this.scheme, this.authority, this.path, query, this.fragment);
} else {
return this;
}
}
public Uri query(String... keyValuePairs) {
return query(UriQuery.from(keyValuePairs));
}
public final String queryPart() {
return this.query.toString();
}
public Uri queryPart(String query) {
return query(UriQuery.parse(query));
}
public Uri updatedQuery(String key, String value) {
return query(this.query.updated(key, value));
}
public Uri removedQuery(String key) {
return query(this.query.removed(key));
}
public Uri appendedQuery(String value) {
return query(this.query.appended(value));
}
public Uri appendedQuery(String key, String value) {
return query(this.query.appended(key, value));
}
public Uri appendedQuery(String... keyValuePairs) {
return query(this.query.appended(keyValuePairs));
}
public Uri appendedQuery(Map<? extends String, ? extends String> params) {
return query(this.query.appended(params));
}
public Uri prependedQuery(String value) {
return query(this.query.prepended(value));
}
public Uri prependedQuery(String key, String value) {
return query(this.query.prepended(key, value));
}
public Uri prependedQuery(String... keyValuePairs) {
return query(this.query.prepended(keyValuePairs));
}
public Uri prependedQuery(Map<? extends String, ? extends String> params) {
return query(this.query.prepended(query));
}
public final UriFragment fragment() {
return this.fragment;
}
public Uri fragment(UriFragment fragment) {
if (fragment != this.fragment) {
return copy(this.scheme, this.authority, this.path, this.query, fragment);
} else {
return this;
}
}
public final String fragmentPart() {
return this.fragment.toString();
}
public Uri fragmentPart(String fragment) {
return fragment(UriFragment.parse(fragment));
}
public final String fragmentIdentifier() {
return this.fragment.identifier();
}
public Uri fragmentIdentifier(String fragment) {
return fragment(UriFragment.from(fragment));
}
public Uri endpoint() {
if (this.path.isDefined() || this.query.isDefined() || this.fragment.isDefined()) {
return Uri.from(this.scheme, this.authority);
} else {
return this;
}
}
public Uri resolve(Uri relative) {
if (relative.scheme.isDefined()) {
return copy(relative.scheme,
relative.authority,
relative.path.removeDotSegments(),
relative.query,
relative.fragment);
} else if (relative.authority.isDefined()) {
return copy(this.scheme,
relative.authority,
relative.path.removeDotSegments(),
relative.query,
relative.fragment);
} else if (relative.path.isEmpty()) {
return copy(this.scheme,
this.authority,
this.path,
relative.query.isDefined() ? relative.query : this.query,
relative.fragment);
} else if (relative.path.isAbsolute()) {
return copy(this.scheme,
this.authority,
relative.path.removeDotSegments(),
relative.query,
relative.fragment);
} else {
return copy(this.scheme,
this.authority,
merge(relative.path).removeDotSegments(),
relative.query,
relative.fragment);
}
}
protected UriPath merge(UriPath relative) {
if (this.authority.isDefined() && this.path.isEmpty()) {
return relative.prependedSlash();
} else if (this.path.isEmpty()) {
return relative;
} else {
return this.path.merge(relative);
}
}
public Uri unresolve(Uri absolute) {
if (!this.scheme.equals(absolute.scheme) || !this.authority.equals(absolute.authority)) {
return absolute;
} else {
return copy(UriScheme.undefined(),
UriAuthority.undefined(),
this.path.unmerge(absolute.path),
absolute.query,
absolute.fragment);
}
}
protected Uri copy(UriScheme scheme, UriAuthority authority, UriPath path,
UriQuery query, UriFragment fragment) {
return Uri.from(scheme, authority, path, query, fragment);
}
@Override
public final int compareTo(Uri that) {
return toString().compareTo(that.toString());
}
@Override
public final boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof Uri) {
return toString().equals(((Uri) other).toString());
}
return false;
}
@Override
public final int hashCode() {
return Murmur3.seed(toString());
}
@Override
public void debug(Output<?> output) {
output = output.write("Uri").write('.');
if (isDefined()) {
output = output.write("parse").write('(').write('"').display(this).write('"').write(')');
} else {
output = output.write("empty").write('(').write(')');
}
}
@Override
public void display(Output<?> output) {
if (this.string != null) {
output = output.write(this.string);
} else {
if (this.scheme.isDefined()) {
output.display(this.scheme).write(':');
}
if (this.authority.isDefined()) {
output = output.write('/').write('/').display(this.authority);
}
output.display(this.path);
if (this.query.isDefined()) {
output = output.write('?').display(this.query);
}
if (this.fragment.isDefined()) {
output = output.write('#').display(this.fragment);
}
}
}
@Override
public final String toString() {
if (this.string == null) {
this.string = Format.display(this);
}
return this.string;
}
private static Uri empty;
private static UriParser standardParser;
public static Uri empty() {
if (empty == null) {
empty = new Uri(UriScheme.undefined(), UriAuthority.undefined(), UriPath.empty(),
UriQuery.undefined(), UriFragment.undefined());
}
return empty;
}
public static Uri from(UriScheme scheme, UriAuthority authority, UriPath path,
UriQuery query, UriFragment fragment) {
if (scheme == null) {
scheme = UriScheme.undefined();
}
if (authority == null) {
authority = UriAuthority.undefined();
}
if (path == null) {
path = UriPath.empty();
}
if (query == null) {
query = UriQuery.undefined();
}
if (fragment == null) {
fragment = UriFragment.undefined();
}
if (scheme.isDefined() || authority.isDefined() || path.isDefined()
|| query.isDefined() || fragment.isDefined()) {
return new Uri(scheme, authority, path, query, fragment);
} else {
return empty();
}
}
public static Uri from(UriScheme scheme, UriAuthority authority, UriPath path, UriQuery query) {
return from(scheme, authority, path, query, null);
}
public static Uri from(UriScheme scheme, UriAuthority authority, UriPath path, UriFragment fragment) {
return from(scheme, authority, path, null, fragment);
}
public static Uri from(UriScheme scheme, UriAuthority authority, UriPath path) {
return from(scheme, authority, path, null, null);
}
public static Uri from(UriScheme scheme, UriAuthority authority) {
return from(scheme, authority, null, null, null);
}
public static Uri from(UriPath path, UriQuery query, UriFragment fragment) {
return from(null, null, path, query, fragment);
}
public static Uri from(UriPath path, UriQuery query) {
return from(null, null, path, query, null);
}
public static Uri from(UriPath path, UriFragment fragment) {
return from(null, null, path, null, fragment);
}
public static Uri from(UriPath path) {
return from(null, null, path, null, null);
}
public static UriParser standardParser() {
if (standardParser == null) {
standardParser = new UriParser();
}
return standardParser;
}
public static Uri parse(String string) {
return standardParser().parseAbsoluteString(string);
}
static boolean isUnreservedChar(int c) {
return c >= 'A' && c <= 'Z'
|| c >= 'a' && c <= 'z'
|| c >= '0' && c <= '9'
|| c == '-' || c == '.'
|| c == '_' || c == '~';
}
static boolean isSubDelimChar(int c) {
return c == '!' || c == '$'
|| c == '&' || c == '('
|| c == ')' || c == '*'
|| c == '+' || c == ','
|| c == ';' || c == '='
|| c == '\'';
}
static boolean isSchemeChar(int c) {
return c >= 'A' && c <= 'Z'
|| c >= 'a' && c <= 'z'
|| c >= '0' && c <= '9'
|| c == '+' || c == '-'
|| c == '.';
}
static boolean isUserInfoChar(int c) {
return isUnreservedChar(c) || isSubDelimChar(c) || c == ':';
}
static boolean isUserChar(int c) {
return isUnreservedChar(c) || isSubDelimChar(c);
}
static boolean isHostChar(int c) {
return isUnreservedChar(c) || isSubDelimChar(c);
}
static boolean isPathChar(int c) {
return isUnreservedChar(c) || isSubDelimChar(c) || c == ':' || c == '@';
}
static boolean isQueryChar(int c) {
return isUnreservedChar(c) || isSubDelimChar(c)
|| c == '/' || c == ':'
|| c == '?' || c == '@';
}
static boolean isParamChar(int c) {
return isUnreservedChar(c)
|| c == '!' || c == '$'
|| c == '(' || c == ')'
|| c == '*' || c == '+'
|| c == ',' || c == '/'
|| c == ':' || c == ';'
|| c == '?' || c == '@'
|| c == '\'';
}
static boolean isFragmentChar(int c) {
return isUnreservedChar(c) || isSubDelimChar(c)
|| c == '/' || c == ':'
|| c == '?' || c == '@';
}
static boolean isAlpha(int c) {
return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z';
}
static void writeScheme(String scheme, Output<?> output) {
final int n = scheme.length();
for (int i = 0; i < n; i = scheme.offsetByCodePoints(i, 1)) {
final int c = scheme.codePointAt(i);
if (i > 0 && isSchemeChar(c) || i == 0 && isAlpha(c)) {
output = output.write(c);
} else {
throw new UriException("Invalid scheme: " + scheme);
}
}
}
static void writeUserInfo(String userInfo, Output<?> output) {
final int n = userInfo.length();
for (int i = 0; i < n; i = userInfo.offsetByCodePoints(i, 1)) {
final int c = userInfo.codePointAt(i);
if (isUserInfoChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeUser(String user, Output<?> output) {
final int n = user.length();
for (int i = 0; i < n; i = user.offsetByCodePoints(i, 1)) {
final int c = user.codePointAt(i);
if (isUserChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeHost(String address, Output<?> output) {
final int n = address.length();
for (int i = 0; i < n; i = address.offsetByCodePoints(i, 1)) {
final int c = address.codePointAt(i);
if (isHostChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeHostLiteral(String address, Output<?> output) {
final int n = address.length();
for (int i = 0; i < n; i = address.offsetByCodePoints(i, 1)) {
final int c = address.codePointAt(i);
if (isHostChar(c) || c == ':') {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writePathSegment(String segment, Output<?> output) {
final int n = segment.length();
for (int i = 0; i < n; i = segment.offsetByCodePoints(i, 1)) {
final int c = segment.codePointAt(i);
if (isPathChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeQuery(String query, Output<?> output) {
final int n = query.length();
for (int i = 0; i < n; i = query.offsetByCodePoints(i, 1)) {
final int c = query.codePointAt(i);
if (isQueryChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeParam(String param, Output<?> output) {
final int n = param.length();
for (int i = 0; i < n; i = param.offsetByCodePoints(i, 1)) {
final int c = param.codePointAt(i);
if (isParamChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeFragment(String fragment, Output<?> output) {
final int n = fragment.length();
for (int i = 0; i < n; i = fragment.offsetByCodePoints(i, 1)) {
final int c = fragment.codePointAt(i);
if (isFragmentChar(c)) {
output = output.write(c);
} else {
writeEncoded(c, output);
}
}
}
static void writeEncoded(int c, Output<?> output) {
if (c == 0x00) { // modified UTF-8
writePctEncoded(0xC0, output);
writePctEncoded(0x80, output);
} else if (c >= 0x00 && c <= 0x7F) { // U+0000..U+007F
writePctEncoded(c, output);
} else if (c >= 0x80 && c <= 0x07FF) { // U+0080..U+07FF
writePctEncoded(0xC0 | (c >>> 6), output);
writePctEncoded(0x80 | (c & 0x3F), output);
} else if (c >= 0x0800 && c <= 0xffff) { // (U+0800..U+D7FF, U+E000..U+FFFF, and surrogates
writePctEncoded(0xE0 | (c >>> 12), output);
writePctEncoded(0x80 | (c >>> 6 & 0x3F), output);
writePctEncoded(0x80 | (c & 0x3F), output);
} else if (c >= 0x10000 && c <= 0x10FFFF) { // U+10000..U+10FFFF
writePctEncoded(0xF0 | (c >>> 18), output);
writePctEncoded(0x80 | (c >>> 12 & 0x3F), output);
writePctEncoded(0x80 | (c >>> 6 & 0x3F), output);
writePctEncoded(0x80 | (c & 0x3F), output);
} else { // surrogate or invalid code point
writePctEncoded(0xEF, output);
writePctEncoded(0xBF, output);
writePctEncoded(0xBD, output);
}
}
static void writePctEncoded(int c, Output<?> output) {
output = output.write('%').write(Base16.lowercase().encodeDigit(c >>> 4 & 0xF))
.write(Base16.lowercase().encodeDigit(c & 0xF));
}
private static Form<Uri> form;
@Kind
public static Form<Uri> form() {
if (form == null) {
form = new UriForm(Uri.empty());
}
return form;
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriAbsoluteParser.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.uri;
import swim.codec.Diagnostic;
import swim.codec.Input;
import swim.codec.Parser;
final class UriAbsoluteParser extends Parser<Uri> {
final UriParser uri;
final Parser<UriScheme> schemeParser;
final Parser<UriAuthority> authorityParser;
final Parser<UriPath> pathParser;
final Parser<UriQuery> queryParser;
final Parser<UriFragment> fragmentParser;
final int step;
UriAbsoluteParser(UriParser uri, Parser<UriScheme> schemeParser,
Parser<UriAuthority> authorityParser, Parser<UriPath> pathParser,
Parser<UriQuery> queryParser, Parser<UriFragment> fragmentParser, int step) {
this.uri = uri;
this.schemeParser = schemeParser;
this.authorityParser = authorityParser;
this.pathParser = pathParser;
this.queryParser = queryParser;
this.fragmentParser = fragmentParser;
this.step = step;
}
UriAbsoluteParser(UriParser uri) {
this(uri, null, null, null, null, null, 1);
}
@Override
public Parser<Uri> feed(Input input) {
return parse(input, this.uri, this.schemeParser, this.authorityParser,
this.pathParser, this.queryParser, this.fragmentParser, this.step);
}
static Parser<Uri> parse(Input input, UriParser uri, Parser<UriScheme> schemeParser,
Parser<UriAuthority> authorityParser, Parser<UriPath> pathParser,
Parser<UriQuery> queryParser, Parser<UriFragment> fragmentParser, int step) {
int c = 0;
if (step == 1) {
if (input.isCont()) {
Input look = input.clone();
while (look.isCont()) {
c = look.head();
if (Uri.isSchemeChar(c)) {
look = look.step();
} else {
break;
}
}
if (look.isCont() && c == ':') {
step = 2;
} else {
step = 3;
}
} else if (input.isDone()) {
step = 3;
}
}
if (step == 2) {
if (schemeParser == null) {
schemeParser = uri.parseScheme(input);
} else {
schemeParser = schemeParser.feed(input);
}
if (schemeParser.isDone()) {
if (input.isCont() && input.head() == ':') {
input = input.step();
step = 3;
} else if (!input.isEmpty()) {
return error(Diagnostic.expected(':', input));
}
} else if (schemeParser.isError()) {
return schemeParser.asError();
}
}
if (step == 3) {
if (input.isCont()) {
c = input.head();
if (c == '/') {
input = input.step();
step = 4;
} else if (c == '?') {
input = input.step();
step = 7;
} else if (c == '#') {
input = input.step();
step = 8;
} else {
step = 6;
}
} else if (input.isDone()) {
return done(uri.absolute(schemeParser != null ? schemeParser.bind() : null,
null, null, null, null));
}
}
if (step == 4) {
if (input.isCont() && input.head() == '/') {
input = input.step();
step = 5;
} else if (input.isCont()) {
final UriPathBuilder pathBuilder = uri.pathBuilder();
pathBuilder.addSlash();
pathParser = uri.parsePath(input, pathBuilder);
step = 6;
} else if (input.isDone()) {
return done(uri.absolute(schemeParser != null ? schemeParser.bind() : null,
null, UriPath.slash(), null, null));
}
}
if (step == 5) {
if (authorityParser == null) {
authorityParser = uri.parseAuthority(input);
} else {
authorityParser = authorityParser.feed(input);
}
if (authorityParser.isDone()) {
if (input.isCont()) {
c = input.head();
if (c == '?') {
input = input.step();
step = 7;
} else if (c == '#') {
input = input.step();
step = 8;
} else {
step = 6;
}
} else if (input.isDone()) {
return done(uri.absolute(schemeParser != null ? schemeParser.bind() : null,
authorityParser.bind(), null, null, null));
}
} else if (authorityParser.isError()) {
return authorityParser.asError();
}
}
if (step == 6) {
if (pathParser == null) {
pathParser = uri.parsePath(input);
} else {
pathParser = pathParser.feed(input);
}
if (pathParser.isDone()) {
if (input.isCont() && input.head() == '?') {
input = input.step();
step = 7;
} else if (input.isCont() && input.head() == '#') {
input = input.step();
step = 8;
} else if (!input.isEmpty()) {
return done(uri.absolute(schemeParser != null ? schemeParser.bind() : null,
authorityParser != null ? authorityParser.bind() : null,
pathParser.bind(), null, null));
}
} else if (pathParser.isError()) {
return pathParser.asError();
}
}
if (step == 7) {
if (queryParser == null) {
queryParser = uri.parseQuery(input);
} else {
queryParser = queryParser.feed(input);
}
if (queryParser.isDone()) {
if (input.isCont() && input.head() == '#') {
input = input.step();
step = 8;
} else if (!input.isEmpty()) {
return done(uri.absolute(schemeParser != null ? schemeParser.bind() : null,
authorityParser != null ? authorityParser.bind() : null,
pathParser != null ? pathParser.bind() : null,
queryParser.bind(), null));
}
} else if (queryParser.isError()) {
return queryParser.asError();
}
}
if (step == 8) {
if (fragmentParser == null) {
fragmentParser = uri.parseFragment(input);
} else {
fragmentParser = fragmentParser.feed(input);
}
if (fragmentParser.isDone()) {
return done(uri.absolute(schemeParser != null ? schemeParser.bind() : null,
authorityParser != null ? authorityParser.bind() : null,
pathParser != null ? pathParser.bind() : null,
queryParser != null ? queryParser.bind() : null,
fragmentParser.bind()));
} else if (fragmentParser.isError()) {
return fragmentParser.asError();
}
}
if (input.isError()) {
return error(input.trap());
}
return new UriAbsoluteParser(uri, schemeParser, authorityParser, pathParser,
queryParser, fragmentParser, step);
}
static Parser<Uri> parse(Input input, UriParser uri) {
return parse(input, uri, null, null, null, null, null, 1);
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriAuthority.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.uri;
import swim.codec.Debug;
import swim.codec.Display;
import swim.codec.Format;
import swim.codec.Output;
import swim.util.Murmur3;
public class UriAuthority implements Comparable<UriAuthority>, Debug, Display {
protected final UriUser user;
protected final UriHost host;
protected final UriPort port;
String string;
protected UriAuthority(UriUser user, UriHost host, UriPort port) {
this.user = user;
this.host = host;
this.port = port;
}
public final boolean isDefined() {
return this.user.isDefined() || this.host.isDefined() || this.port.isDefined();
}
public final UriUser user() {
return this.user;
}
public UriAuthority user(UriUser user) {
if (user != this.user) {
return copy(user, this.host, this.port);
} else {
return this;
}
}
public final String userPart() {
return this.user.toString();
}
public UriAuthority userPart(String user) {
return user(UriUser.parse(user));
}
public String username() {
return this.user.username();
}
public UriAuthority username(String username) {
return user(this.user.username(username));
}
public UriAuthority username(String username, String password) {
return user(UriUser.from(username, password));
}
public String password() {
return this.user.password();
}
public UriAuthority password(String password) {
return user(this.user.password(password));
}
public final UriHost host() {
return this.host;
}
public UriAuthority host(UriHost host) {
if (host != this.host) {
return copy(this.user, host, this.port);
} else {
return this;
}
}
public final String hostPart() {
return this.host.toString();
}
public UriAuthority hostPart(String host) {
return host(UriHost.parse(host));
}
public final String hostAddress() {
return this.host.address();
}
public final String hostName() {
return this.host.name();
}
public UriAuthority hostName(String address) {
return host(UriHost.name(address));
}
public final String hostIPv4() {
return this.host.ipv4();
}
public UriAuthority hostIPv4(String address) {
return host(UriHost.ipv4(address));
}
public final String hostIPv6() {
return this.host.ipv6();
}
public UriAuthority hostIPv6(String address) {
return host(UriHost.ipv6(address));
}
public final UriPort port() {
return this.port;
}
public UriAuthority port(UriPort port) {
if (port != this.port) {
return copy(this.user, this.host, port);
} else {
return this;
}
}
public final String portPart() {
return this.port.toString();
}
public UriAuthority portPart(String port) {
return port(UriPort.parse(port));
}
public final int portNumber() {
return this.port.number();
}
public UriAuthority portNumber(int number) {
return port(UriPort.from(number));
}
protected UriAuthority copy(UriUser user, UriHost host, UriPort port) {
return UriAuthority.from(user, host, port);
}
@Override
public final int compareTo(UriAuthority that) {
return toString().compareTo(that.toString());
}
@Override
public final boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof UriAuthority) {
return toString().equals(((UriAuthority) other).toString());
}
return false;
}
@Override
public final int hashCode() {
return Murmur3.seed(toString());
}
@Override
public void debug(Output<?> output) {
output = output.write("UriAuthority").write('.');
if (isDefined()) {
output = output.write("parse").write('(').write('"').display(this).write('"').write(')');
} else {
output = output.write("undefined").write('(').write(')');
}
}
@Override
public void display(Output<?> output) {
if (this.string != null) {
output = output.write(this.string);
} else {
if (this.user.isDefined()) {
output.display(this.user).write('@');
}
output.display(this.host);
if (this.port.isDefined()) {
output = output.write(':').display(this.port);
}
}
}
@Override
public final String toString() {
if (this.string == null) {
this.string = Format.display(this);
}
return this.string;
}
private static UriAuthority undefined;
public static UriAuthority undefined() {
if (undefined == null) {
undefined = new UriAuthority(UriUser.undefined(), UriHost.undefined(), UriPort.undefined());
}
return undefined;
}
public static UriAuthority from(UriUser user, UriHost host, UriPort port) {
if (user == null) {
user = UriUser.undefined();
}
if (host == null) {
host = UriHost.undefined();
}
if (port == null) {
port = UriPort.undefined();
}
if (user.isDefined() || host.isDefined() || port.isDefined()) {
return new UriAuthority(user, host, port);
} else {
return undefined();
}
}
public static UriAuthority from(UriHost host, UriPort port) {
return from(null, host, port);
}
public static UriAuthority from(UriHost host) {
return from(null, host, null);
}
public static UriAuthority parse(String string) {
return Uri.standardParser().parseAuthorityString(string);
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriAuthorityLiteral.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.uri;
import swim.collections.HashTrieMap;
final class UriAuthorityLiteral extends UriAuthorityPattern {
final UriAuthority authority;
final UriPathPattern rest;
UriAuthorityLiteral(UriAuthority authority, UriPathPattern rest) {
this.authority = authority;
this.rest = rest;
}
@Override
public boolean isUri() {
return this.rest.isUri();
}
@Override
public Uri toUri() {
return this.rest.toUri();
}
@Override
Uri apply(UriScheme scheme, String[] args, int index) {
return this.rest.apply(scheme, this.authority, args, index);
}
@Override
HashTrieMap<String, String> unapply(UriAuthority authority, UriPath path, UriQuery query,
UriFragment fragment, HashTrieMap<String, String> args) {
if (this.authority.equals(authority)) {
return this.rest.unapply(path, query, fragment, args);
} else {
return args;
}
}
@Override
boolean matches(UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment) {
if (this.authority.equals(authority)) {
return this.rest.matches(path, query, fragment);
} else {
return false;
}
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriAuthorityMapper.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.uri;
abstract class UriAuthorityMapper<T> extends UriSchemeMapper<T> {
abstract T get(UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment);
@Override
T get(UriScheme scheme, UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment) {
return get(authority, path, query, fragment);
}
abstract UriAuthorityMapper<T> merged(UriAuthorityMapper<T> that);
@Override
UriSchemeMapper<T> merged(UriSchemeMapper<T> that) {
if (that instanceof UriAuthorityMapper<?>) {
return merged((UriAuthorityMapper<T>) that);
} else {
return that;
}
}
abstract UriAuthorityMapper<T> removed(UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment);
@Override
UriSchemeMapper<T> removed(UriScheme scheme, UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment) {
return removed(authority, path, query, fragment);
}
abstract UriAuthorityMapper<T> unmerged(UriAuthorityMapper<T> that);
@Override
UriSchemeMapper<T> unmerged(UriSchemeMapper<T> that) {
if (that instanceof UriAuthorityMapper<?>) {
return unmerged((UriAuthorityMapper<T>) that);
} else {
return this;
}
}
static <T> UriAuthorityMapper<T> compile(Uri pattern, UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment, T value) {
return UriPathMapper.compile(pattern, path, query, fragment, value);
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriAuthorityParser.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.uri;
import swim.codec.Diagnostic;
import swim.codec.Input;
import swim.codec.Parser;
final class UriAuthorityParser extends Parser<UriAuthority> {
final UriParser uri;
final Parser<UriUser> userParser;
final Parser<UriHost> hostParser;
final Parser<UriPort> portParser;
final int step;
UriAuthorityParser(UriParser uri, Parser<UriUser> userParser, Parser<UriHost> hostParser,
Parser<UriPort> portParser, int step) {
this.uri = uri;
this.userParser = userParser;
this.hostParser = hostParser;
this.portParser = portParser;
this.step = step;
}
UriAuthorityParser(UriParser uri) {
this(uri, null, null, null, 1);
}
@Override
public Parser<UriAuthority> feed(Input input) {
return parse(input, this.uri, this.userParser, this.hostParser, this.portParser, this.step);
}
static Parser<UriAuthority> parse(Input input, UriParser uri, Parser<UriUser> userParser,
Parser<UriHost> hostParser, Parser<UriPort> portParser, int step) {
int c = 0;
if (step == 1) {
if (input.isCont()) {
Input look = input.clone();
while (look.isCont()) {
c = look.head();
if (c != '@' && c != '/') {
look = look.step();
} else {
break;
}
}
if (look.isCont() && c == '@') {
step = 2;
} else {
step = 3;
}
} else if (input.isDone()) {
step = 3;
}
}
if (step == 2) {
if (userParser == null) {
userParser = uri.parseUser(input);
} else {
userParser = userParser.feed(input);
}
if (userParser.isDone()) {
if (input.isCont() && input.head() == '@') {
input = input.step();
step = 3;
} else if (!input.isEmpty()) {
return error(Diagnostic.expected('@', input));
}
} else if (userParser.isError()) {
return userParser.asError();
}
}
if (step == 3) {
if (hostParser == null) {
hostParser = uri.parseHost(input);
} else {
hostParser = hostParser.feed(input);
}
if (hostParser.isDone()) {
if (input.isCont() && input.head() == ':') {
input = input.step();
step = 4;
} else if (!input.isEmpty()) {
return done(uri.authority(userParser != null ? userParser.bind() : null,
hostParser.bind(), null));
}
} else if (hostParser.isError()) {
return hostParser.asError();
}
}
if (step == 4) {
if (portParser == null) {
portParser = uri.parsePort(input);
} else {
portParser = portParser.feed(input);
}
if (portParser.isDone()) {
return done(uri.authority(userParser != null ? userParser.bind() : null,
hostParser != null ? hostParser.bind() : null,
portParser.bind()));
} else if (portParser.isError()) {
return portParser.asError();
}
}
if (input.isError()) {
return error(input.trap());
}
return new UriAuthorityParser(uri, userParser, hostParser, portParser, step);
}
static Parser<UriAuthority> parse(Input input, UriParser uri) {
return parse(input, uri, null, null, null, 1);
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriAuthorityPattern.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.uri;
import swim.collections.HashTrieMap;
abstract class UriAuthorityPattern extends UriSchemePattern {
abstract HashTrieMap<String, String> unapply(UriAuthority authority, UriPath path,
UriQuery query, UriFragment fragment,
HashTrieMap<String, String> args);
@Override
HashTrieMap<String, String> unapply(UriScheme scheme, UriAuthority authority,
UriPath path, UriQuery query, UriFragment fragment,
HashTrieMap<String, String> args) {
return unapply(authority, path, query, fragment, args);
}
abstract boolean matches(UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment);
@Override
boolean matches(UriScheme scheme, UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment) {
if (!scheme.isDefined()) {
return matches(authority, path, query, fragment);
} else {
return false;
}
}
static UriAuthorityPattern compile(Uri pattern, UriAuthority authority, UriPath path,
UriQuery query, UriFragment fragment) {
if (authority.isDefined()) {
return new UriAuthorityLiteral(authority, UriPathPattern.compile(pattern, path, query, fragment));
} else {
return UriPathPattern.compile(pattern, path, query, fragment);
}
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriConstantMapping.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.uri;
import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import swim.util.Murmur3;
final class UriConstantMapping<T> extends UriTerminalMapper<T> {
final Uri key;
final T value;
UriConstantMapping(Uri key, T value) {
this.key = key;
this.value = value;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public int size() {
return 1;
}
@Override
public boolean containsValue(Object value) {
return value == null ? this.value == null : value.equals(this.value);
}
@Override
public T get() {
return this.value;
}
@Override
public Iterator<Entry<Uri, T>> iterator() {
return new UriConstantMappingIterator<T>(this.key, this.value);
}
@Override
public Iterator<Uri> keyIterator() {
return new UriConstantMappingKeyIterator(this.key);
}
@Override
public Iterator<T> valueIterator() {
return new UriConstantMappingValueIterator<T>(this.value);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (other instanceof UriConstantMapping<?>) {
final UriConstantMapping<?> that = (UriConstantMapping<?>) other;
return this.value.equals(that.value);
}
return false;
}
@Override
public int hashCode() {
if (hashSeed == 0) {
hashSeed = Murmur3.seed(UriConstantMapping.class);
}
return Murmur3.mash(Murmur3.mix(hashSeed, this.value.hashCode()));
}
private static int hashSeed;
}
final class UriConstantMappingIterator<T> implements Iterator<Map.Entry<Uri, T>> {
Uri key;
T value;
UriConstantMappingIterator(Uri key, T value) {
this.key = key;
this.value = value;
}
@Override
public boolean hasNext() {
return this.key != null;
}
@Override
public Map.Entry<Uri, T> next() {
final Uri key = this.key;
if (key == null) {
throw new NoSuchElementException();
}
final Map.Entry<Uri, T> entry = new AbstractMap.SimpleImmutableEntry<Uri, T>(key, value);
this.key = null;
value = null;
return entry;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
final class UriConstantMappingKeyIterator implements Iterator<Uri> {
Uri key;
UriConstantMappingKeyIterator(Uri key) {
this.key = key;
}
@Override
public boolean hasNext() {
return this.key != null;
}
@Override
public Uri next() {
final Uri key = this.key;
if (key == null) {
throw new NoSuchElementException();
}
this.key = null;
return key;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
final class UriConstantMappingValueIterator<T> implements Iterator<T> {
T value;
boolean hasNext;
UriConstantMappingValueIterator(T value) {
this.value = value;
this.hasNext = true;
}
@Override
public boolean hasNext() {
return this.hasNext;
}
@Override
public T next() {
if (!this.hasNext) {
throw new NoSuchElementException();
}
final T value = this.value;
this.hasNext = false;
this.value = null;
return value;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriConstantPattern.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.uri;
import swim.collections.HashTrieMap;
final class UriConstantPattern extends UriTerminalPattern {
final Uri uri;
UriConstantPattern(Uri uri) {
this.uri = uri;
}
@Override
public boolean isUri() {
return true;
}
@Override
public Uri toUri() {
return this.uri;
}
@Override
Uri apply(UriScheme scheme, UriAuthority authority, UriPath path, UriQuery query, UriFragment fragment, String[] args, int index) {
if (index < args.length) {
throw new IllegalArgumentException("applied " + index + " of " + args.length + " interpolation arguments");
}
return new Uri(scheme, authority, path, query, fragment);
}
@Override
HashTrieMap<String, String> unapply(HashTrieMap<String, String> args) {
return args;
}
@Override
boolean matches() {
return true;
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriException.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.uri;
public class UriException extends RuntimeException {
private static final long serialVersionUID = 1L;
public UriException(String message, Throwable cause) {
super(message, cause);
}
public UriException(String message) {
super(message);
}
public UriException(Throwable cause) {
super(cause);
}
public UriException() {
super();
}
}
|
0 | java-sources/ai/swim/swim-uri/3.10.0/swim | java-sources/ai/swim/swim-uri/3.10.0/swim/uri/UriForm.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.uri;
import swim.codec.ParserException;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Text;
import swim.structure.Value;
public class UriForm extends Form<Uri> {
final Uri unit;
UriForm(Uri unit) {
this.unit = unit;
}
@Override
public Uri unit() {
return this.unit;
}
@Override
public Form<Uri> unit(Uri unit) {
return new UriForm(unit);
}
@Override
public Class<Uri> type() {
return Uri.class;
}
@Override
public Item mold(Uri value) {
if (value != null) {
return Text.from(value.toString());
} else {
return Item.extant();
}
}
@Override
public Uri cast(Item item) {
final Value value = item.target();
try {
final String string = value.stringValue();
if (string != null) {
return Uri.parse(string);
}
} catch (UnsupportedOperationException | ParserException | UriException e) {
// swallow
}
return null;
}
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.