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.
// Copyright 2015-2024 Nstream, 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.system.downlink;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import swim.api.DownlinkException;
import swim.collections.BTreeMap;
import swim.concurrent.Cont;
import swim.concurrent.Stage;
import swim.structure.Attr;
import swim.structure.Form;
import swim.structure.Record;
import swim.structure.Value;
import swim.system.DownlinkModel;
import swim.system.DownlinkRelay;
import swim.system.DownlinkView;
import swim.system.Push;
import swim.system.warp.MapDownlinkModem;
import swim.uri.Uri;
import swim.util.Cursor;
import swim.util.OrderedMap;
import swim.util.OrderedMapCursor;
import swim.warp.EventMessage;
public class MapDownlinkModel extends MapDownlinkModem> {
protected final BTreeMap state;
protected int flags;
public MapDownlinkModel(Uri meshUri, Uri hostUri, Uri nodeUri, Uri laneUri,
float prio, float rate, Value body) {
super(meshUri, hostUri, nodeUri, laneUri, prio, rate, body);
this.state = BTreeMap.empty();
this.flags = 0;
}
public final boolean isStateful() {
return (this.flags & MapDownlinkModel.STATEFUL) != 0;
}
public MapDownlinkModel isStateful(boolean isStateful) {
if (isStateful) {
this.flags |= MapDownlinkModel.STATEFUL;
} else {
this.flags &= ~MapDownlinkModel.STATEFUL;
}
final Object views = DownlinkModel.VIEWS.get(this);
if (views instanceof DownlinkView) {
((MapDownlinkView, ?>) views).didSetStateful(isStateful);
} else if (views instanceof DownlinkView[]) {
final DownlinkView[] viewArray = (DownlinkView[]) views;
for (int i = 0, n = viewArray.length; i < n; i += 1) {
((MapDownlinkView, ?>) viewArray[i]).didSetStateful(isStateful);
}
}
return this;
}
@Override
protected void pushDownEvent(Push push) {
final EventMessage message = push.message();
this.onEvent(message);
final Value payload = message.body();
final String tag = payload.tag();
if ("update".equals(tag)) {
final Value header = payload.header("update");
final Value key = header.get("key");
final Value value = payload.body();
new MapDownlinkRelayUpdate(this, message, push.cont(), key, value).run();
} else if ("remove".equals(tag)) {
final Value header = payload.header("remove");
final Value key = header.get("key");
new MapDownlinkRelayRemove(this, message, push.cont(), key).run();
} else if ("drop".equals(tag)) {
final Value header = payload.header("drop");
final int lower = header.intValue(0);
new MapDownlinkRelayDrop(this, message, push.cont(), lower).run();
} else if ("take".equals(tag)) {
final Value header = payload.header("take");
final int upper = header.intValue(0);
new MapDownlinkRelayTake(this, message, push.cont(), upper).run();
} else if ("clear".equals(tag)) {
new MapDownlinkRelayClear(this, message, push.cont()).run();
} else {
push.trap(new DownlinkException("unknown subcommand: " + payload));
}
}
@Override
protected Value nextUpKey(Value key) {
final Value value = this.state.get(key);
if (value != null) {
return Attr.of("update", Record.create(1).slot("key", key)).concat(value);
} else {
return null;
}
}
@Override
protected void didAddDownlink(MapDownlinkView, ?> view) {
super.didAddDownlink(view);
if (DownlinkModel.VIEWS.get(this) instanceof DownlinkView) {
this.isStateful(((MapDownlinkView, ?>) view).isStateful());
}
}
public boolean isEmpty() {
return this.state.isEmpty();
}
public int size() {
return this.state.size();
}
public boolean containsKey(Object key) {
if (key != null) {
return this.state.containsKey(key);
} else {
return false;
}
}
public boolean containsValue(Object value) {
if (value != null) {
return this.state.containsValue(value);
} else {
return false;
}
}
public int indexOf(Object key) {
return this.state.indexOf(key);
}
public Value get(Object key) {
if (key != null) {
final Value value = this.state.get(key);
if (value != null) {
return value;
}
}
return Value.absent();
}
public Map.Entry getEntry(Object key) {
return this.state.getEntry(key);
}
public Map.Entry getIndex(int index) {
return this.state.getIndex(index);
}
public Map.Entry firstEntry() {
return this.state.firstEntry();
}
public Value firstKey() {
return this.state.firstKey();
}
public Value firstValue() {
return this.state.firstValue();
}
public Map.Entry lastEntry() {
return this.state.lastEntry();
}
public Value lastKey() {
return this.state.lastKey();
}
public Value lastValue() {
return this.state.lastValue();
}
public Map.Entry nextEntry(Value key) {
return this.state.nextEntry(key);
}
public Value nextKey(Value key) {
return this.state.nextKey(key);
}
public Value nextValue(Value key) {
return this.state.nextValue(key);
}
public Map.Entry previousEntry(Value key) {
return this.state.previousEntry(key);
}
public Value previousKey(Value key) {
return this.state.previousKey(key);
}
public Value previousValue(Value key) {
return this.state.previousValue(key);
}
@SuppressWarnings("unchecked")
public V put(MapDownlinkView view, K keyObject, V newObject) {
final Form keyForm = view.keyForm;
final Form valueForm = view.valueForm;
final Value key = keyForm.mold(keyObject).toValue();
final Value newValue = valueForm.mold(newObject).toValue();
final MapDownlinkRelayUpdate relay = new MapDownlinkRelayUpdate(this, view.stage(), key, newValue);
relay.keyForm = (Form