Skip to content

Commit

Permalink
concept thing
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Dec 14, 2022
1 parent 2d56feb commit 9934aaa
Show file tree
Hide file tree
Showing 52 changed files with 683 additions and 337 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.github.linyuzai.thing.core;

import com.github.linyuzai.thing.core.concept.Attribute;
import com.github.linyuzai.thing.core.action.ThingAction;
import com.github.linyuzai.thing.core.concept.Category;
import com.github.linyuzai.thing.core.concept.Thing;

import java.util.Map;
import java.util.function.Function;

public class Test {

public void test0(Thing thing) {
thing.getAttributes().get("color").update("RED").perform().toEvent().publish();
thing.getAttributes().get("color").update("RED").execute().toEvent().publish();
}

public void test1(Thing thing, Map<String, Object> attributes) {
thing.getAttributes().update(attributes).perform().toEvent().publish();
thing.getAttributes().update(attributes).execute().toEvent().publish();
}

public void test2(Thing device) {
public void test2(Thing thing) {
thing.getCategories().add("A", new Function<Category, ThingAction>() {
@Override
public ThingAction apply(Category category) {
return category.getCategories().add("B");
}
}).execute();
}

public void test3(Thing thing) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.function.Supplier;

@Deprecated
@Getter
@RequiredArgsConstructor
public abstract class ContextThingAction implements ThingAction {
Expand All @@ -21,7 +22,7 @@ public static ThingAction of(ThingContext context, Supplier<Supplier<ThingEvent>
return new ContextThingAction(context) {

@Override
public ThingActionPerformance perform() {
public ThingActionExecution execute() {
Supplier<ThingEvent> event = supplier.get();
return event::get;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package com.github.linyuzai.thing.core.action;

import com.github.linyuzai.thing.core.event.ThingEvent;

import java.util.function.Supplier;

public interface ThingAction {

ThingActionPerformance perform();
String ADD = "ThingAction::ADD";

String REMOVE = "ThingAction::REMOVE";

String VALUE_UPDATE = "ThingAction::VALUE_UPDATE";

ThingActionExecution execute();

//ThingActionChain chain();

static ThingAction create(Supplier<Supplier<ThingEvent>> supplier) {
return () -> {
Supplier<ThingEvent> event = supplier.get();
return event::get;
};
}


ThingActionChain chain();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@
public interface ThingActionChain extends ThingAction {

ThingActionChain next(ThingAction action);

@Override
default ThingActionChain chain() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public ThingActionChain next(ThingAction action) {
}

@Override
public ThingActionPerformance perform() {
List<ThingActionPerformance> performances = new ArrayList<>();
public ThingActionExecution execute() {
List<ThingActionExecution> executions = new ArrayList<>();
for (ThingAction action : actions) {
ThingActionPerformance performance = action.perform();
performances.add(performance);
ThingActionExecution execution = action.execute();
executions.add(execution);
}
return new ThingActionPerformances(context, performances);
return new ThingActionExecutions(context, executions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.linyuzai.thing.core.event.ThingEvent;

public interface ThingActionPerformance {
public interface ThingActionExecution {

ThingEvent toEvent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
import com.github.linyuzai.thing.core.context.ThingContext;
import com.github.linyuzai.thing.core.event.ThingEvent;
import com.github.linyuzai.thing.core.event.ThingEvents;
import com.github.linyuzai.thing.core.util.ListWrapper;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@Getter
@RequiredArgsConstructor
public class ThingActionPerformances implements ThingActionPerformance {
public class ThingActionExecutions implements ListWrapper<ThingActionExecution>, ThingActionExecution {

@Getter
private final ThingContext context;

private final List<ThingActionPerformance> performances;
private final List<ThingActionExecution> executions;

@Override
public ThingEvent toEvent() {
List<ThingEvent> events = performances.stream()
.map(ThingActionPerformance::toEvent)
List<ThingEvent> events = executions.stream()
.map(ThingActionExecution::toEvent)
.collect(Collectors.toList());
return new ThingEvents(context, events);
}

@Override
public List<ThingActionExecution> unwrap() {
return executions;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
package com.github.linyuzai.thing.core.concept;

import com.github.linyuzai.thing.core.action.ContextThingAction;
import com.github.linyuzai.thing.core.action.ThingAction;
import com.github.linyuzai.thing.core.event.AttributeUpdatedEvent;
import com.github.linyuzai.thing.core.event.ThingUpdatedEvent;
import lombok.Getter;
import lombok.Setter;

public abstract class AbstractAttribute implements Attribute {
@Getter
@Setter
public abstract class AbstractAttribute extends AbstractIdentify<Attribute> implements Attribute {

private Label label;

private Thing thing;

private Object value;

@Override
public ThingAction update(Object value) {
return ContextThingAction.of(getThing().getContext(), () -> {
return ThingAction.create(() -> {
Object oldValue = getValue();
doUpdate(value);
return () -> new AttributeUpdatedEvent(this, oldValue, value);
return () -> new ThingUpdatedEvent(getContext(), ThingAction.VALUE_UPDATE, this, this, oldValue, value);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
package com.github.linyuzai.thing.core.concept;

public abstract class AbstractCategory implements Category {
import com.github.linyuzai.thing.core.container.Categories;
import com.github.linyuzai.thing.core.container.Labels;
import com.github.linyuzai.thing.core.factory.CategoryFactory;
import com.github.linyuzai.thing.core.factory.LabelFactory;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class AbstractCategory extends AbstractIdentify<Category> implements Category {

private String name;

private Category parent;

private Categories categories;

private Labels labels;

@Override
public Categories getCategories() {
if (categories == null) {
CategoryFactory factory = getContext().get(CategoryFactory.class);
categories = factory.createContainer();
categories.setContext(getContext());
}
return categories;
}

@Override
public Labels getLabels() {
if (labels == null) {
LabelFactory factory = getContext().get(LabelFactory.class);
labels = factory.createContainer();
labels.setContext(getContext());
labels.setCategory(this);
}
return labels;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.linyuzai.thing.core.concept;

import com.github.linyuzai.thing.core.context.ThingContext;
import com.github.linyuzai.thing.core.generate.IdentifyGenerator;
import lombok.Getter;
import lombok.Setter;

import java.util.function.Function;

@Getter
@Setter
@SuppressWarnings("unchecked")
public abstract class AbstractIdentify<T extends Identify<T>> implements Identify<T> {

private String id;

private Function<T, String> idProvider = t -> {
IdentifyGenerator generator = getContext().get(IdentifyGenerator.class);
return generator.generateId(t);
};

private String key;

private Function<T, String> keyProvider = t -> {
IdentifyGenerator generator = getContext().get(IdentifyGenerator.class);
return generator.generateKey(t);
};

private ThingContext context;

@Override
public String getId() {
if (id == null) {
if (idProvider != null) {
id = idProvider.apply((T) this);
}
}
return id;
}

@Override
public String getKey() {
if (key == null) {
if (keyProvider != null) {
key = keyProvider.apply((T) this);
}
}
return key;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package com.github.linyuzai.thing.core.concept;

public abstract class AbstractLabel implements Label {
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class AbstractLabel extends AbstractIdentify<Label> implements Label {

private String name;

private Category category;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
package com.github.linyuzai.thing.core.concept;

public abstract class AbstractRelationship implements Relationship {
import com.github.linyuzai.thing.core.factory.RelationshipFactory;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public abstract class AbstractRelationship extends AbstractIdentify<Relationship> implements Relationship {

private String name;

private Thing thing;

private Thing relation;

private Relationship opposite;

public Relationship getOpposite() {
if (opposite == null) {
RelationshipFactory factory = getContext().get(RelationshipFactory.class);
opposite = factory.create();
String suffix = ".OPPOSITE";
if (name.endsWith(suffix)) {
opposite.setName(name.substring(0, name.length() - suffix.length()));
} else {
opposite.setName(name + suffix);
}
opposite.setThing(relation);
opposite.setRelation(thing);
opposite.setOpposite(this);
opposite.setContext(getContext());
}
return opposite;
}
}
Loading

0 comments on commit 9934aaa

Please sign in to comment.