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.lane;
import java.util.ListIterator;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import swim.api.LaneException;
import swim.api.Link;
import swim.api.data.ListData;
import swim.collections.FingerTrieSeq;
import swim.concurrent.Cont;
import swim.concurrent.Stage;
import swim.structure.Form;
import swim.structure.Value;
import swim.system.LaneModel;
import swim.system.LaneRelay;
import swim.system.LaneView;
import swim.system.Push;
import swim.system.WarpBinding;
import swim.system.warp.ListLinkDelta;
import swim.system.warp.WarpLaneModel;
import swim.warp.CommandMessage;
public class ListLaneModel extends WarpLaneModel, ListLaneUplink> {
protected int flags;
protected ListData data;
ListLaneModel(int flags) {
this.flags = flags;
this.data = null;
}
public ListLaneModel() {
this(0);
}
@Override
public String laneType() {
return "list";
}
@Override
protected ListLaneUplink createWarpUplink(WarpBinding link) {
return new ListLaneUplink(this, link, this.createUplinkAddress(link));
}
@Override
protected void didOpenLaneView(ListLaneView> view) {
view.setLaneBinding(this);
}
@Override
public void onCommand(Push push) {
this.awaitStart();
final CommandMessage message = push.message();
final Value payload = message.body();
final String tag = payload.tag();
if ("update".equals(tag)) {
final Value header = payload.header("update");
final int index = header.get("index").intValue(-1);
if (index > -1) {
final Object key;
if (header.get("key").isDistinct()) {
key = header.get("key");
} else {
key = null;
}
final Value value = payload.body();
new ListLaneRelayUpdate(this, message, push.cont(), index, value, key).run();
}
} else if ("move".equals(tag)) {
final Value header = payload.header("move");
final int fromIndex = header.get("from").intValue(-1);
final int toIindex = header.get("to").intValue(-1);
if (fromIndex > -1 && toIindex > -1) {
final Object key;
if (header.get("key").isDistinct()) {
key = header.get("key");
} else {
key = null;
}
new ListLaneRelayMove(this, message, push.cont(), fromIndex, toIindex, key).run();
}
} else if ("remove".equals(tag)) {
final Value header = payload.header("remove");
final int index = header.get("index").intValue(-1);
if (index > -1) {
final Object key;
if (header.get("key").isDistinct()) {
key = header.get("key");
} else {
key = null;
}
new ListLaneRelayRemove(this, message, push.cont(), index, key).run();
}
} else if ("drop".equals(tag)) {
final Value header = payload.header("drop");
final int lower = header.intValue(0);
new ListLaneRelayDrop(this, message, push.cont(), lower).run();
} else if ("take".equals(tag)) {
final Value header = payload.header("take");
final int upper = header.intValue(0);
new ListLaneRelayTake(this, message, push.cont(), upper).run();
} else if ("clear".equals(tag)) {
new ListLaneRelayClear(this, message, push.cont()).run();
} else {
push.trap(new LaneException("unknown subcommand: " + payload));
}
}
@SuppressWarnings("unchecked")
protected void sendDown(ListLinkDelta delta) {
FingerTrieSeq uplinks;
FingerTrieSeq closedLinks = FingerTrieSeq.empty();
do {
uplinks = (FingerTrieSeq) LaneModel.UPLINKS.get(this);
for (int i = 0, n = uplinks.size(); i < n; i += 1) {
final ListLaneUplink uplink = uplinks.get(i);
if (uplink.isConnected()) {
uplink.sendDown(delta);
} else {
closedLinks = closedLinks.appended(uplink.linkKey());
}
}
} while (uplinks != LaneModel.UPLINKS.get(this));
for (Value linkKey : closedLinks) {
this.closeUplink(linkKey);
}
}
public final boolean isResident() {
return (this.flags & ListLaneModel.RESIDENT) != 0;
}
public ListLaneModel isResident(boolean isResident) {
if (this.data != null) {
this.data.isResident(isResident);
}
if (isResident) {
this.flags |= ListLaneModel.RESIDENT;
} else {
this.flags &= ~ListLaneModel.RESIDENT;
}
final Object views = LaneModel.VIEWS.get(this);
if (views instanceof ListLaneView>) {
((ListLaneView>) views).didSetResident(isResident);
} else if (views instanceof LaneView[]) {
final LaneView[] viewArray = (LaneView[]) views;
for (int i = 0, n = viewArray.length; i < n; i += 1) {
((ListLaneView>) viewArray[i]).didSetResident(isResident);
}
}
return this;
}
public final boolean isTransient() {
return (this.flags & ListLaneModel.TRANSIENT) != 0;
}
public ListLaneModel isTransient(boolean isTransient) {
if (this.data != null) {
this.data.isTransient(isTransient);
}
if (isTransient) {
this.flags |= ListLaneModel.TRANSIENT;
} else {
this.flags &= ~ListLaneModel.TRANSIENT;
}
final Object views = LaneModel.VIEWS.get(this);
if (views instanceof ListLaneView>) {
((ListLaneView>) views).didSetTransient(isTransient);
} else if (views instanceof LaneView[]) {
final LaneView[] viewArray = (LaneView[]) views;
for (int i = 0, n = viewArray.length; i < n; i += 1) {
((ListLaneView>) viewArray[i]).didSetTransient(isTransient);
}
}
return this;
}
@SuppressWarnings("unchecked")
public boolean add(ListLaneView view, int index, V newObject) {
return this.add(view, index, newObject, null);
}
@SuppressWarnings("unchecked")
public boolean add(ListLaneView view, int index, V newObject, Object key) {
final Form valueForm = view.valueForm;
final Value newValue = valueForm.mold(newObject).toValue();
final ListLaneRelayUpdate relay = new ListLaneRelayUpdate(this, this.stage(), index, newValue, key);
relay.valueForm = (Form