Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Feb 28, 2024
2 parents 67543cb + 18c7f43 commit 35584af
Show file tree
Hide file tree
Showing 21 changed files with 1,705 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cqfn/astranaut/core/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public String getProperty(final String name) {

@Override
public Builder createBuilder() {
return null;
return new Constructor();
}
}

Expand Down
71 changes: 70 additions & 1 deletion src/main/java/org/cqfn/astranaut/core/DifferenceNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
package org.cqfn.astranaut.core;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
* Node containing child nodes, as well as actions on these nodes.
Expand Down Expand Up @@ -117,6 +119,73 @@ public Node getAfter() {
return this.getBranch(DifferenceTreeItem::getAfter);
}

/**
* Adds an action that inserts the node after another node.
* If no other node is specified, inserts at the beginning of the children's list.
* @param node Node to be inserted
* @param after Node after which to insert
* @return Result of operation, @return {@code true} if action was added
*/
public boolean insertNodeAfter(final Node node, final Node after) {
boolean result = false;
if (after == null) {
this.children.add(0, new Insert(node));
result = true;
} else {
final ListIterator<DifferenceTreeItem> iterator = this.children.listIterator();
while (iterator.hasNext()) {
final Node child = iterator.next();
if (child instanceof DifferenceNode
&& ((DifferenceNode) child).getPrototype() == after) {
iterator.add(new Insert(node));
result = true;
break;
}
}
}
return result;
}

/**
* Adds an action that replaces a node.
* The position of the node is specified by the index.
* @param index Node index
* @param replacement Child node to be replaced by
* @return Result of operation, @return {@code true} if action was added
*/
public boolean replaceNode(final int index, final Node replacement) {
boolean result = false;
if (index >= 0 && index < this.children.size()) {
final DifferenceTreeItem child = this.children.get(index);
if (child instanceof DifferenceNode) {
this.children.set(
index,
new Replace(
((DifferenceNode) child).getPrototype(),
replacement
)
);
result = true;
}
}
return result;
}

/**
* Adds an action that replaces a node.
* @param node A node
* @param replacement Child node to be replaced by
* @return Result of operation, @return {@code true} if action was added
*/
public boolean replaceNode(final Node node, final Node replacement) {
boolean result = false;
final int index = this.findChildIndex(node);
if (index >= 0) {
result = this.replaceNode(index, replacement);
}
return result;
}

/**
* Adds an action that removes a node by index.
* @param index Node index
Expand Down Expand Up @@ -154,7 +223,7 @@ public boolean deleteNode(final Node node) {
*/
private List<DifferenceTreeItem> initChildrenList() {
final int count = this.prototype.getChildCount();
final List<DifferenceTreeItem> result = new ArrayList<>(count);
final List<DifferenceTreeItem> result = new LinkedList<>();
for (int index = 0; index < count; index = index + 1) {
result.add(
new DifferenceNode(this, this.prototype.getChild(index))
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/org/cqfn/astranaut/core/Factory.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,21 @@ public final Builder createBuilder(final String name) {
final Type type = this.types.get(name);
result = type.createBuilder();
} else {
if (name.equals("Delete")) {
result = new Delete.Constructor();
} else {
final DraftNode.Constructor draft = new DraftNode.Constructor();
draft.setName(name);
result = draft;
switch (name) {
case "Insert":
result = new Insert.Constructor();
break;
case "Replace":
result = new Replace.Constructor();
break;
case "Delete":
result = new Delete.Constructor();
break;
default:
final DraftNode.Constructor draft = new DraftNode.Constructor();
draft.setName(name);
result = draft;
break;
}
}
return result;
Expand Down
220 changes: 220 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/Insert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 Ivan Kniazkov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.cqfn.astranaut.core;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* An action that inserts a child element.
*
* @since 1.1.0
*/
public final class Insert implements Action {
/**
* The type.
*/
public static final Type TYPE = new InsertType();

/**
* Child element.
*/
private final Node child;

/**
* Constructor.
* @param child A child element that will be added.
*/
public Insert(final Node child) {
this.child = child;
}

@Override
public Node getBefore() {
return null;
}

@Override
public Node getAfter() {
return this.child;
}

@Override
public Fragment getFragment() {
return this.child.getFragment();
}

@Override
public Type getType() {
return Insert.TYPE;
}

@Override
public String getData() {
return "";
}

@Override
public int getChildCount() {
return 1;
}

@Override
public Node getChild(final int index) {
final Node node;
if (index == 0) {
node = this.child;
} else {
node = null;
}
return node;
}

/**
* Type of 'Insert' action.
*
* @since 1.1.0
*/
private static final class InsertType implements Type {
/**
* The 'Node' string.
*/
private static final String NODE = "Node";

/**
* The 'ACTION' string.
*/
private static final String ACTION = "Action";

/**
* The 'DELETE' string.
*/
private static final String INSERT = "Insert";

/**
* The list of child descriptors.
*/
private static final List<ChildDescriptor> CHILDREN =
Collections.singletonList(
new ChildDescriptor(
InsertType.NODE,
false
)
);

/**
* Hierarchy.
*/
private static final List<String> HIERARCHY =
Collections.unmodifiableList(
Arrays.asList(
InsertType.INSERT,
InsertType.ACTION
)
);

/**
* Properties.
*/
private static final Map<String, String> PROPERTIES = Stream.of(
new String[][] {
{"color", "blue"},
}).collect(Collectors.toMap(data -> data[0], data -> data[1]));

@Override
public String getName() {
return InsertType.INSERT;
}

@Override
public List<ChildDescriptor> getChildTypes() {
return InsertType.CHILDREN;
}

@Override
public List<String> getHierarchy() {
return InsertType.HIERARCHY;
}

@Override
public String getProperty(final String name) {
return InsertType.PROPERTIES.getOrDefault(name, "");
}

@Override
public Builder createBuilder() {
return new Constructor();
}
}

/**
* Class for 'Delete' action construction.
*
* @since 1.1.0
*/
public static final class Constructor implements Builder {
/**
* Child node.
*/
private Node child;

@Override
public void setFragment(final Fragment fragment) {
// do nothing
}

@Override
public boolean setData(final String str) {
return str.isEmpty();
}

@Override
public boolean setChildrenList(final List<Node> list) {
boolean result = false;
if (list.size() == 1) {
this.child = list.get(0);
result = true;
}
return result;
}

@Override
public boolean isValid() {
return this.child != null;
}

@Override
public Node createNode() {
Node node = EmptyTree.INSTANCE;
if (this.isValid()) {
node = new Insert(this.child);
}
return node;
}
}
}
Loading

0 comments on commit 35584af

Please sign in to comment.