@Test public void decode() throws DecodingException { Command decoded = new DefaultCommandCodec().decode(domain, commandModel); assertThat(decoded, commandEqualTo(editCommand)); }
@Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public Command decode(final EditingDomain domain, final CCommand command) throws DecodingException { Command result; switch (command.getType()) { case COMPOUND: CCompoundCommand ccompound = (CCompoundCommand) command; CompoundCommand compound = new CompoundCommand(); for (CCommand next : ccompound.getCommands()) { compound.append(decode(domain, next)); } result = compound; break; case ADD: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Collection<?> values; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); values = command.getDataValues().stream() .map(v -> EcoreUtil.createFromString(dataType, v)).collect(toList()); } else { values = command.getObjectValues(); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = AddCommand.create(domain, owner, feature, values, getFirst(command.getIndices(), index)); break; } case REMOVE: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); if (!command.getIndices().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, Ints.toArray(command.getIndices())); } else if (!command.getObjectValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getObjectValues()); } else if (!command.getDataValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getDataValues()); } else { throw new DecodingException("incomplete remove command specification"); } break; } case SET: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Object value; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); value = command.getDataValues().isEmpty() ? null : EcoreUtil.createFromString(dataType, command.getDataValues().get(0)); } else { value = getFirst(command.getObjectValues(), null); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = SetCommand.create(domain, owner, feature, value, index); break; } case MOVE: { throw new DecodingException("todo"); } case REPLACE: { throw new DecodingException("todo"); } default: throw new DecodingException("unsupported command type: " + command.getType().getLiteral()); } return result; }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public Command decode(final EditingDomain domain, final CCommand command) throws DecodingException { Command result; switch (command.getType()) { case COMPOUND: CCompoundCommand ccompound = (CCompoundCommand) command; CompoundCommand compound = new CompoundCommand(); for (CCommand next : ccompound.getCommands()) { compound.append(decode(domain, next)); } result = compound; break; case ADD: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Collection<?> values; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); values = command.getDataValues().stream() .map(v -> EcoreUtil.createFromString(dataType, v)).collect(toList()); } else { values = command.getObjectValues(); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = AddCommand.create(domain, owner, feature, values, getFirst(command.getIndices(), index)); break; } case REMOVE: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); if (!command.getIndices().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, Ints.toArray(command.getIndices())); } else if (!command.getObjectValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getObjectValues()); } else if (!command.getDataValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getDataValues()); } else { throw new DecodingException("incomplete remove command specification"); } break; } case SET: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Object value; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); value = command.getDataValues().isEmpty() ? null : EcoreUtil.createFromString(dataType, command.getDataValues().get(0)); } else { value = getFirst(command.getObjectValues(), null); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = SetCommand.create(domain, owner, feature, value, index); break; } case MOVE: { throw new DecodingException("todo"); } case REPLACE: { throw new DecodingException("todo"); } default: throw new DecodingException("unsupported command type: " + command.getType().getLiteral()); } return result; } }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public Command decode(final EditingDomain domain, final CCommand command) throws DecodingException { Command result; switch (command.getType()) { case COMPOUND: CCompoundCommand ccompound = (CCompoundCommand) command; CompoundCommand compound = new CompoundCommand(); for (CCommand next : ccompound.getCommands()) { compound.append(decode(domain, next)); } result = compound; break; case ADD: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Collection<?> values; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); values = command.getDataValues().stream() .map(v -> EcoreUtil.createFromString(dataType, v)).collect(toList()); } else { values = command.getObjectValues(); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = AddCommand.create(domain, owner, feature, values, getFirst(command.getIndices(), index)); break; } case REMOVE: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); if (!command.getIndices().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, Ints.toArray(command.getIndices())); } else if (!command.getObjectValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getObjectValues()); } else if (!command.getDataValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getDataValues()); } else { throw new DecodingException("incomplete remove command specification"); } break; } case SET: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Object value; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); value = command.getDataValues().isEmpty() ? null : EcoreUtil.createFromString(dataType, command.getDataValues().get(0)); } else { value = getFirst(command.getObjectValues(), null); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = SetCommand.create(domain, owner, feature, value, index); break; } case MOVE: { throw new DecodingException("todo"); } case REPLACE: { throw new DecodingException("todo"); } default: throw new DecodingException("unsupported command type: " + command.getType().getLiteral()); } return result; } DefaultCommandCodec(); }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public Command decode(final EditingDomain domain, final CCommand command) throws DecodingException { Command result; switch (command.getType()) { case COMPOUND: CCompoundCommand ccompound = (CCompoundCommand) command; CompoundCommand compound = new CompoundCommand(); for (CCommand next : ccompound.getCommands()) { compound.append(decode(domain, next)); } result = compound; break; case ADD: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Collection<?> values; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); values = command.getDataValues().stream() .map(v -> EcoreUtil.createFromString(dataType, v)).collect(toList()); } else { values = command.getObjectValues(); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = AddCommand.create(domain, owner, feature, values, getFirst(command.getIndices(), index)); break; } case REMOVE: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); if (!command.getIndices().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, Ints.toArray(command.getIndices())); } else if (!command.getObjectValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getObjectValues()); } else if (!command.getDataValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getDataValues()); } else { throw new DecodingException("incomplete remove command specification"); } break; } case SET: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Object value; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); value = command.getDataValues().isEmpty() ? null : EcoreUtil.createFromString(dataType, command.getDataValues().get(0)); } else { value = getFirst(command.getObjectValues(), null); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = SetCommand.create(domain, owner, feature, value, index); break; } case MOVE: { throw new DecodingException("todo"); } case REPLACE: { throw new DecodingException("todo"); } default: throw new DecodingException("unsupported command type: " + command.getType().getLiteral()); } return result; } DefaultCommandCodec(); @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) CCommand encode(final Command command); @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) Command decode(final EditingDomain domain, final CCommand command); }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public Command decode(final EditingDomain domain, final CCommand command) throws DecodingException { Command result; switch (command.getType()) { case COMPOUND: CCompoundCommand ccompound = (CCompoundCommand) command; CompoundCommand compound = new CompoundCommand(); for (CCommand next : ccompound.getCommands()) { compound.append(decode(domain, next)); } result = compound; break; case ADD: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Collection<?> values; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); values = command.getDataValues().stream() .map(v -> EcoreUtil.createFromString(dataType, v)).collect(toList()); } else { values = command.getObjectValues(); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = AddCommand.create(domain, owner, feature, values, getFirst(command.getIndices(), index)); break; } case REMOVE: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); if (!command.getIndices().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, Ints.toArray(command.getIndices())); } else if (!command.getObjectValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getObjectValues()); } else if (!command.getDataValues().isEmpty()) { result = RemoveCommand.create(domain, owner, feature, command.getDataValues()); } else { throw new DecodingException("incomplete remove command specification"); } break; } case SET: { EObject owner = command.getOwner(); EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature()); Object value; if (feature instanceof EAttribute) { EDataType dataType = ((EAttribute) feature).getEAttributeType(); value = command.getDataValues().isEmpty() ? null : EcoreUtil.createFromString(dataType, command.getDataValues().get(0)); } else { value = getFirst(command.getObjectValues(), null); } int index = command.getIndices().isEmpty() ? NO_INDEX : command.getIndices().get(0); result = SetCommand.create(domain, owner, feature, value, index); break; } case MOVE: { throw new DecodingException("todo"); } case REPLACE: { throw new DecodingException("todo"); } default: throw new DecodingException("unsupported command type: " + command.getType().getLiteral()); } return result; } DefaultCommandCodec(); @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) CCommand encode(final Command command); @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) Command decode(final EditingDomain domain, final CCommand command);  }