Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
swim.runtime.lane.JoinValueLaneView Maven / Gradle / Ivy
Go to download
Uploads all artifacts belonging to configuration ':swim-runtime:archives'
// 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.runtime.lane;
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import swim.api.Link;
import swim.api.SwimContext;
import swim.api.agent.AgentContext;
import swim.api.data.MapData;
import swim.api.downlink.ValueDownlink;
import swim.api.http.function.DecodeRequestHttp;
import swim.api.http.function.DidRequestHttp;
import swim.api.http.function.DidRespondHttp;
import swim.api.http.function.DoRespondHttp;
import swim.api.http.function.WillRequestHttp;
import swim.api.http.function.WillRespondHttp;
import swim.api.lane.JoinValueLane;
import swim.api.lane.Lane;
import swim.api.lane.function.DidCommand;
import swim.api.lane.function.DidDownlinkValue;
import swim.api.lane.function.DidEnter;
import swim.api.lane.function.DidLeave;
import swim.api.lane.function.DidUplink;
import swim.api.lane.function.WillCommand;
import swim.api.lane.function.WillDownlinkValue;
import swim.api.lane.function.WillEnter;
import swim.api.lane.function.WillLeave;
import swim.api.lane.function.WillUplink;
import swim.concurrent.Conts;
import swim.observable.function.DidClear;
import swim.observable.function.DidRemoveKey;
import swim.observable.function.DidUpdateKey;
import swim.observable.function.WillClear;
import swim.observable.function.WillRemoveKey;
import swim.observable.function.WillUpdateKey;
import swim.runtime.LaneContext;
import swim.structure.Form;
import swim.structure.Value;
import swim.structure.collections.ValueEntryIterator;
import swim.structure.collections.ValueIterator;
import swim.uri.Uri;
public class JoinValueLaneView extends LaneView implements JoinValueLane {
protected final AgentContext agentContext;
protected Form keyForm;
protected Form valueForm;
protected int flags;
protected JoinValueLaneModel laneBinding;
protected MapData dataView;
JoinValueLaneView(AgentContext agentContext, Form keyForm, Form valueForm,
int flags, Object observers) {
super(observers);
this.agentContext = agentContext;
this.keyForm = keyForm;
this.valueForm = valueForm;
this.flags = flags;
}
public JoinValueLaneView(AgentContext agentContext, Form keyForm, Form valueForm) {
this(agentContext, keyForm, valueForm, RESIDENT, null);
}
@Override
public AgentContext agentContext() {
return this.agentContext;
}
@Override
public JoinValueLaneModel getLaneBinding() {
return this.laneBinding;
}
void setLaneBinding(JoinValueLaneModel laneBinding) {
this.laneBinding = laneBinding;
this.dataView = laneBinding.data.keyForm(this.keyForm).valueForm(this.valueForm);
}
@Override
public JoinValueLaneModel createLaneBinding() {
return new JoinValueLaneModel(this.flags);
}
@Override
public final Form keyForm() {
return this.keyForm;
}
@Override
public JoinValueLaneView keyForm(Form keyForm) {
return new JoinValueLaneView(this.agentContext, keyForm, this.valueForm,
this.flags, typesafeObservers(this.observers));
}
@Override
public JoinValueLaneView keyClass(Class keyClass) {
return keyForm(Form.forClass(keyClass));
}
public void setKeyForm(Form keyForm) {
this.keyForm = keyForm;
}
@Override
public final Form valueForm() {
return this.valueForm;
}
@Override
public JoinValueLaneView valueForm(Form valueForm) {
return new JoinValueLaneView(this.agentContext, this.keyForm, valueForm,
this.flags, typesafeObservers(this.observers));
}
@Override
public JoinValueLaneView valueClass(Class valueClass) {
return valueForm(Form.forClass(valueClass));
}
public void setValueForm(Form valueForm) {
this.valueForm = valueForm;
}
protected Object typesafeObservers(Object observers) {
// TODO: filter out WillDownlinkValue, DidDownlinkValue, WillUpdateKey, DidUpdateKey,
// WillRemoveKey, DidRemoveKey, WillClear, DidClear
return observers;
}
public final boolean isResident() {
return (this.flags & RESIDENT) != 0;
}
@Override
public JoinValueLaneView isResident(boolean isResident) {
if (isResident) {
this.flags |= RESIDENT;
} else {
this.flags &= ~RESIDENT;
}
final JoinValueLaneModel laneBinding = this.laneBinding;
if (laneBinding != null) {
laneBinding.isResident(isResident);
}
return this;
}
void didSetResident(boolean isResident) {
if (isResident) {
this.flags |= RESIDENT;
} else {
this.flags &= ~RESIDENT;
}
}
public final boolean isTransient() {
return (this.flags & TRANSIENT) != 0;
}
@Override
public JoinValueLaneView isTransient(boolean isTransient) {
if (isTransient) {
this.flags |= TRANSIENT;
} else {
this.flags &= ~TRANSIENT;
}
final JoinValueLaneModel laneBinding = this.laneBinding;
if (laneBinding != null) {
laneBinding.isTransient(isTransient);
}
return this;
}
void didSetTransient(boolean isTransient) {
if (isTransient) {
this.flags |= TRANSIENT;
} else {
this.flags &= ~TRANSIENT;
}
}
@Override
public final boolean isSigned() {
return (this.flags & SIGNED) != 0;
}
@Override
public JoinValueLaneView isSigned(boolean isSigned) {
didSetSigned(isSigned);
// note: marked final given access of concurrently accessed volatile objects
final JoinValueLaneModel laneBinding = this.laneBinding;
if (laneBinding != null) {
laneBinding.isSigned(isSigned);
}
return this;
}
void didSetSigned(boolean isSigned) {
if (isSigned) {
this.flags |= SIGNED;
} else {
this.flags &= ~SIGNED;
}
}
@Override
public void close() {
this.laneBinding.closeLaneView(this);
}
@SuppressWarnings("unchecked")
@Override
public JoinValueLaneView observe(Object observer) {
return (JoinValueLaneView) super.observe(observer);
}
@SuppressWarnings("unchecked")
@Override
public JoinValueLaneView unobserve(Object observer) {
return (JoinValueLaneView) super.unobserve(observer);
}
@Override
public JoinValueLaneView willDownlink(WillDownlinkValue willDownlink) {
return observe(willDownlink);
}
@Override
public JoinValueLaneView didDownlink(DidDownlinkValue didDownlink) {
return observe(didDownlink);
}
@Override
public JoinValueLaneView willUpdate(WillUpdateKey willUpdate) {
return observe(willUpdate);
}
@Override
public JoinValueLaneView didUpdate(DidUpdateKey didUpdate) {
return observe(didUpdate);
}
@Override
public JoinValueLaneView willRemove(WillRemoveKey willRemove) {
return observe(willRemove);
}
@Override
public JoinValueLaneView didRemove(DidRemoveKey didRemove) {
return observe(didRemove);
}
@Override
public JoinValueLaneView willClear(WillClear willClear) {
return observe(willClear);
}
@Override
public JoinValueLaneView didClear(DidClear didClear) {
return observe(didClear);
}
@Override
public JoinValueLaneView willCommand(WillCommand willCommand) {
return observe(willCommand);
}
@Override
public JoinValueLaneView didCommand(DidCommand didCommand) {
return observe(didCommand);
}
@Override
public JoinValueLaneView willUplink(WillUplink willUplink) {
return observe(willUplink);
}
@Override
public JoinValueLaneView didUplink(DidUplink didUplink) {
return observe(didUplink);
}
@Override
public JoinValueLaneView willEnter(WillEnter willEnter) {
return observe(willEnter);
}
@Override
public JoinValueLaneView didEnter(DidEnter didEnter) {
return observe(didEnter);
}
@Override
public JoinValueLaneView willLeave(WillLeave willLeave) {
return observe(willLeave);
}
@Override
public JoinValueLaneView didLeave(DidLeave didLeave) {
return observe(didLeave);
}
@Override
public JoinValueLaneView decodeRequest(DecodeRequestHttp decodeRequest) {
return observe(decodeRequest);
}
@Override
public JoinValueLaneView willRequest(WillRequestHttp> willRequest) {
return observe(willRequest);
}
@Override
public JoinValueLaneView didRequest(DidRequestHttp didRequest) {
return observe(didRequest);
}
@Override
public JoinValueLaneView doRespond(DoRespondHttp doRespond) {
return observe(doRespond);
}
@Override
public JoinValueLaneView willRespond(WillRespondHttp> willRespond) {
return observe(willRespond);
}
@Override
public JoinValueLaneView didRespond(DidRespondHttp> didRespond) {
return observe(didRespond);
}
@SuppressWarnings("unchecked")
protected Map.Entry dispatchWillUpdate(Link link, K key, V newValue, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLane(this);
SwimContext.setLink(link);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillUpdateKey, ?>) {
if (((WillUpdateKey, ?>) observers).isPreemptive() == preemptive) {
try {
newValue = ((WillUpdateKey) observers).willUpdate(key, newValue);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillUpdateKey, ?>) {
if (((WillUpdateKey, ?>) observer).isPreemptive() == preemptive) {
try {
newValue = ((WillUpdateKey) observer).willUpdate(key, newValue);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return new AbstractMap.SimpleImmutableEntry(complete, newValue);
} finally {
SwimContext.setLink(oldLink);
SwimContext.setLane(oldLane);
}
}
@SuppressWarnings("unchecked")
protected boolean dispatchDidUpdate(Link link, K key, V newValue, V oldValue, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLane(this);
SwimContext.setLink(link);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidUpdateKey, ?>) {
if (((DidUpdateKey, ?>) observers).isPreemptive() == preemptive) {
try {
((DidUpdateKey) observers).didUpdate(key, newValue, oldValue);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidUpdateKey, ?>) {
if (((DidUpdateKey, ?>) observer).isPreemptive() == preemptive) {
try {
((DidUpdateKey) observer).didUpdate(key, newValue, oldValue);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
SwimContext.setLane(oldLane);
}
}
@SuppressWarnings("unchecked")
protected boolean dispatchWillRemove(Link link, K key, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLane(this);
SwimContext.setLink(link);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillRemoveKey>) {
if (((WillRemoveKey>) observers).isPreemptive() == preemptive) {
try {
((WillRemoveKey) observers).willRemove(key);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillRemoveKey>) {
if (((WillRemoveKey>) observer).isPreemptive() == preemptive) {
try {
((WillRemoveKey) observer).willRemove(key);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
SwimContext.setLane(oldLane);
}
}
@SuppressWarnings("unchecked")
protected boolean dispatchDidRemove(Link link, K key, V oldValue, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLane(this);
SwimContext.setLink(link);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidRemoveKey, ?>) {
if (((DidRemoveKey, ?>) observers).isPreemptive() == preemptive) {
try {
((DidRemoveKey) observers).didRemove(key, oldValue);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidRemoveKey, ?>) {
if (((DidRemoveKey, ?>) observer).isPreemptive() == preemptive) {
try {
((DidRemoveKey) observer).didRemove(key, oldValue);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
SwimContext.setLane(oldLane);
}
}
protected boolean dispatchWillClear(Link link, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLane(this);
SwimContext.setLink(link);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillClear) {
if (((WillClear) observers).isPreemptive() == preemptive) {
try {
((WillClear) observers).willClear();
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillClear) {
if (((WillClear) observer).isPreemptive() == preemptive) {
try {
((WillClear) observer).willClear();
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
SwimContext.setLane(oldLane);
}
}
protected boolean dispatchDidClear(Link link, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLane(this);
SwimContext.setLink(link);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidClear) {
if (((DidClear) observers).isPreemptive() == preemptive) {
try {
((DidClear) observers).didClear();
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidClear) {
if (((DidClear) observer).isPreemptive() == preemptive) {
try {
((DidClear) observer).didClear();
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
SwimContext.setLane(oldLane);
}
}
@SuppressWarnings("unchecked")
protected Map.Entry> dispatchWillDownlink(K key, ValueDownlink> downlink, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
try {
SwimContext.setLane(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillDownlinkValue>) {
if (((WillDownlinkValue>) observers).isPreemptive() == preemptive) {
try {
downlink = ((WillDownlinkValue) observers).willDownlink(key, downlink);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillDownlinkValue>) {
if (((WillDownlinkValue>) observer).isPreemptive() == preemptive) {
try {
downlink = ((WillDownlinkValue) observer).willDownlink(key, downlink);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return new AbstractMap.SimpleImmutableEntry>(complete, downlink);
} finally {
SwimContext.setLane(oldLane);
}
}
@SuppressWarnings("unchecked")
protected boolean dispatchDidDownlink(K key, ValueDownlink> downlink, boolean preemptive) {
final Lane oldLane = SwimContext.getLane();
try {
SwimContext.setLane(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidDownlinkValue>) {
if (((DidDownlinkValue>) observers).isPreemptive() == preemptive) {
try {
((DidDownlinkValue) observers).didDownlink(key, downlink);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidDownlinkValue>) {
if (((DidDownlinkValue>) observer).isPreemptive() == preemptive) {
try {
((DidDownlinkValue) observer).didDownlink(key, downlink);
} catch (Throwable error) {
if (Conts.isNonFatal(error)) {
laneDidFail(error);
} else {
throw error;
}
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLane(oldLane);
}
}
public ValueDownlink laneWillDownlink(K key, ValueDownlink downlink) {
return downlink;
}
public void laneDidDownlink(K key, ValueDownlink downlink) {
}
public V laneWillUpdate(K key, V newValue) {
return newValue;
}
public void laneDidUpdate(K key, V newValue, V oldValue) {
}
public void laneWillRemove(K key) {
}
public void laneDidRemove(K key, V oldValue) {
}
public void laneWillClear() {
}
public void laneDidClear() {
}
@Override
public ValueDownlink downlink(K key) {
final LaneContext laneContext = this.laneBinding.laneContext;
return new JoinValueLaneDownlink(laneContext, laneContext.stage(), this.laneBinding,
this.keyForm.mold(key).toValue(), this.laneBinding.meshUri(),
Uri.empty(), Uri.empty(), Uri.empty(), 0.0f, 0.0f,
Value.absent(), this.valueForm);
}
@Override
public boolean isEmpty() {
return this.dataView.isEmpty();
}
@Override
public int size() {
return this.dataView.size();
}
@Override
public boolean containsKey(Object key) {
return this.dataView.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return this.dataView.containsValue(value);
}
@Override
public V get(Object key) {
return this.dataView.get(key);
}
@SuppressWarnings("unchecked")
@Override
public ValueDownlink> getDownlink(Object key) {
return this.laneBinding.getDownlink(this.keyForm.mold((K) key));
}
@Override
public V put(K key, V value) {
return this.laneBinding.put(this, key, value);
}
@Override
public void putAll(Map extends K, ? extends V> map) {
for (Map.Entry extends K, ? extends V> entry : map.entrySet()) {
this.laneBinding.put(this, entry.getKey(), entry.getValue());
}
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(key)) {
return this.laneBinding.remove(this, (K) key);
}
return this.valueForm.unit();
}
@Override
public void clear() {
this.laneBinding.clear(this);
}
@SuppressWarnings("unchecked")
@Override
public Set> entrySet() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new JoinValueLaneViewEntrySet(this);
} else {
return (Set>) (Set>) this.laneBinding.entrySet();
}
}
@SuppressWarnings("unchecked")
@Override
public Set keySet() {
if (this.keyForm != Form.forValue()) {
return this.dataView.keySet();
} else {
return (Set) (Set>) this.laneBinding.keySet();
}
}
@SuppressWarnings("unchecked")
@Override
public Collection values() {
if (this.valueForm != Form.forValue()) {
return new JoinValueLaneViewValues(this);
} else {
return (Collection) this.laneBinding.values();
}
}
@SuppressWarnings("unchecked")
@Override
public Iterator> iterator() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new ValueEntryIterator(this.laneBinding.iterator(), this.keyForm, this.valueForm);
} else {
return (Iterator>) (Iterator>) this.laneBinding.iterator();
}
}
@Override
public Iterator keyIterator() {
return this.dataView.keyIterator();
}
@SuppressWarnings("unchecked")
@Override
public Iterator valueIterator() {
if (this.valueForm != Form.forValue()) {
return new ValueIterator(this.laneBinding.valueIterator(), this.valueForm);
} else {
return (Iterator) this.laneBinding.valueIterator();
}
}
@SuppressWarnings("unchecked")
@Override
public Iterator>> downlinkIterator() {
return (Iterator>>) (Iterator>) this.laneBinding.downlinks.iterator();
}
static final int RESIDENT = 1 << 0;
static final int TRANSIENT = 1 << 1;
static final int SIGNED = 1 << 2;
}
final class JoinValueLaneViewEntrySet extends AbstractSet> {
final JoinValueLaneView view;
JoinValueLaneViewEntrySet(JoinValueLaneView view) {
this.view = view;
}
@Override
public int size() {
return this.view.size();
}
@Override
public Iterator> iterator() {
return this.view.iterator();
}
}
final class JoinValueLaneViewValues extends AbstractCollection {
final JoinValueLaneView view;
JoinValueLaneViewValues(JoinValueLaneView view) {
this.view = view;
}
@Override
public int size() {
return this.view.size();
}
@Override
public Iterator iterator() {
return this.view.valueIterator();
}
}