@Test public void encode() throws EncodingException { CCommand encoded = new DefaultCommandCodec().encode(editCommand); assertThat(encoded, EMFMatchers.eEqualTo(commandModel)); }
@Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public CCommand encode(final Command command) throws EncodingException { CCommand result; if (command instanceof CompoundCommand) { CCompoundCommand compound = CCommandFactory.eINSTANCE.createCompoundCommand(); compound.setType(CommandKind.COMPOUND); EList<CCommand> commands = compound.getCommands(); for (Command next : ((CompoundCommand) command).getCommandList()) { commands.add(encode(next)); } result = compound; } else if (command instanceof AddCommand) { AddCommand add = (AddCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.ADD); result.setOwner(add.getOwner()); result.setFeature(add.getFeature().getName()); result.getIndices().add(add.getIndex()); if (add.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) add.getFeature()).getEAttributeType(); collectCommandValues(add.getCollection(), dataType, result); } else { collectCommandEObjects(add.getCollection(), result, true); } } else if (command instanceof RemoveCommand) { RemoveCommand remove = (RemoveCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.REMOVE); result.setOwner(remove.getOwner()); result.setFeature(remove.getFeature().getName()); int[] indices = getIndices(remove); if (indices != null) { result.getIndices().addAll(Ints.asList(indices)); } if (remove.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) remove.getFeature()).getEAttributeType(); collectCommandValues(remove.getCollection(), dataType, result); } else { collectCommandEObjects(remove.getCollection(), result, false); } } else if (command instanceof SetCommand) { SetCommand set = (SetCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.SET); result.setOwner(set.getOwner()); result.setFeature(set.getFeature().getName()); result.getIndices().add(set.getIndex()); if (set.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) set.getFeature()).getEAttributeType(); collectCommandValues(singleton(set.getValue()), dataType, result); } else { collectCommandEObjects(singleton(set.getValue()), result, true); } } else if (command instanceof ReplaceCommand) { throw new EncodingException("todo"); } else if (command instanceof MoveCommand) { throw new EncodingException("todo"); } else { throw new EncodingException("unsupported command type: " + command.getClass().getName()); } return result; }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public CCommand encode(final Command command) throws EncodingException { CCommand result; if (command instanceof CompoundCommand) { CCompoundCommand compound = CCommandFactory.eINSTANCE.createCompoundCommand(); compound.setType(CommandKind.COMPOUND); EList<CCommand> commands = compound.getCommands(); for (Command next : ((CompoundCommand) command).getCommandList()) { commands.add(encode(next)); } result = compound; } else if (command instanceof AddCommand) { AddCommand add = (AddCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.ADD); result.setOwner(add.getOwner()); result.setFeature(add.getFeature().getName()); result.getIndices().add(add.getIndex()); if (add.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) add.getFeature()).getEAttributeType(); collectCommandValues(add.getCollection(), dataType, result); } else { collectCommandEObjects(add.getCollection(), result, true); } } else if (command instanceof RemoveCommand) { RemoveCommand remove = (RemoveCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.REMOVE); result.setOwner(remove.getOwner()); result.setFeature(remove.getFeature().getName()); int[] indices = getIndices(remove); if (indices != null) { result.getIndices().addAll(Ints.asList(indices)); } if (remove.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) remove.getFeature()).getEAttributeType(); collectCommandValues(remove.getCollection(), dataType, result); } else { collectCommandEObjects(remove.getCollection(), result, false); } } else if (command instanceof SetCommand) { SetCommand set = (SetCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.SET); result.setOwner(set.getOwner()); result.setFeature(set.getFeature().getName()); result.getIndices().add(set.getIndex()); if (set.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) set.getFeature()).getEAttributeType(); collectCommandValues(singleton(set.getValue()), dataType, result); } else { collectCommandEObjects(singleton(set.getValue()), result, true); } } else if (command instanceof ReplaceCommand) { throw new EncodingException("todo"); } else if (command instanceof MoveCommand) { throw new EncodingException("todo"); } else { throw new EncodingException("unsupported command type: " + command.getClass().getName()); } return result; } }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public CCommand encode(final Command command) throws EncodingException { CCommand result; if (command instanceof CompoundCommand) { CCompoundCommand compound = CCommandFactory.eINSTANCE.createCompoundCommand(); compound.setType(CommandKind.COMPOUND); EList<CCommand> commands = compound.getCommands(); for (Command next : ((CompoundCommand) command).getCommandList()) { commands.add(encode(next)); } result = compound; } else if (command instanceof AddCommand) { AddCommand add = (AddCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.ADD); result.setOwner(add.getOwner()); result.setFeature(add.getFeature().getName()); result.getIndices().add(add.getIndex()); if (add.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) add.getFeature()).getEAttributeType(); collectCommandValues(add.getCollection(), dataType, result); } else { collectCommandEObjects(add.getCollection(), result, true); } } else if (command instanceof RemoveCommand) { RemoveCommand remove = (RemoveCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.REMOVE); result.setOwner(remove.getOwner()); result.setFeature(remove.getFeature().getName()); int[] indices = getIndices(remove); if (indices != null) { result.getIndices().addAll(Ints.asList(indices)); } if (remove.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) remove.getFeature()).getEAttributeType(); collectCommandValues(remove.getCollection(), dataType, result); } else { collectCommandEObjects(remove.getCollection(), result, false); } } else if (command instanceof SetCommand) { SetCommand set = (SetCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.SET); result.setOwner(set.getOwner()); result.setFeature(set.getFeature().getName()); result.getIndices().add(set.getIndex()); if (set.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) set.getFeature()).getEAttributeType(); collectCommandValues(singleton(set.getValue()), dataType, result); } else { collectCommandEObjects(singleton(set.getValue()), result, true); } } else if (command instanceof ReplaceCommand) { throw new EncodingException("todo"); } else if (command instanceof MoveCommand) { throw new EncodingException("todo"); } else { throw new EncodingException("unsupported command type: " + command.getClass().getName()); } return result; } DefaultCommandCodec(); }
DefaultCommandCodec implements CommandCodec { @Override @SuppressWarnings({ "checkstyle:CyclomaticComplexity", "checkstyle:JavaNCSS" }) public CCommand encode(final Command command) throws EncodingException { CCommand result; if (command instanceof CompoundCommand) { CCompoundCommand compound = CCommandFactory.eINSTANCE.createCompoundCommand(); compound.setType(CommandKind.COMPOUND); EList<CCommand> commands = compound.getCommands(); for (Command next : ((CompoundCommand) command).getCommandList()) { commands.add(encode(next)); } result = compound; } else if (command instanceof AddCommand) { AddCommand add = (AddCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.ADD); result.setOwner(add.getOwner()); result.setFeature(add.getFeature().getName()); result.getIndices().add(add.getIndex()); if (add.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) add.getFeature()).getEAttributeType(); collectCommandValues(add.getCollection(), dataType, result); } else { collectCommandEObjects(add.getCollection(), result, true); } } else if (command instanceof RemoveCommand) { RemoveCommand remove = (RemoveCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.REMOVE); result.setOwner(remove.getOwner()); result.setFeature(remove.getFeature().getName()); int[] indices = getIndices(remove); if (indices != null) { result.getIndices().addAll(Ints.asList(indices)); } if (remove.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) remove.getFeature()).getEAttributeType(); collectCommandValues(remove.getCollection(), dataType, result); } else { collectCommandEObjects(remove.getCollection(), result, false); } } else if (command instanceof SetCommand) { SetCommand set = (SetCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.SET); result.setOwner(set.getOwner()); result.setFeature(set.getFeature().getName()); result.getIndices().add(set.getIndex()); if (set.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) set.getFeature()).getEAttributeType(); collectCommandValues(singleton(set.getValue()), dataType, result); } else { collectCommandEObjects(singleton(set.getValue()), result, true); } } else if (command instanceof ReplaceCommand) { throw new EncodingException("todo"); } else if (command instanceof MoveCommand) { throw new EncodingException("todo"); } else { throw new EncodingException("unsupported command type: " + command.getClass().getName()); } 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 CCommand encode(final Command command) throws EncodingException { CCommand result; if (command instanceof CompoundCommand) { CCompoundCommand compound = CCommandFactory.eINSTANCE.createCompoundCommand(); compound.setType(CommandKind.COMPOUND); EList<CCommand> commands = compound.getCommands(); for (Command next : ((CompoundCommand) command).getCommandList()) { commands.add(encode(next)); } result = compound; } else if (command instanceof AddCommand) { AddCommand add = (AddCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.ADD); result.setOwner(add.getOwner()); result.setFeature(add.getFeature().getName()); result.getIndices().add(add.getIndex()); if (add.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) add.getFeature()).getEAttributeType(); collectCommandValues(add.getCollection(), dataType, result); } else { collectCommandEObjects(add.getCollection(), result, true); } } else if (command instanceof RemoveCommand) { RemoveCommand remove = (RemoveCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.REMOVE); result.setOwner(remove.getOwner()); result.setFeature(remove.getFeature().getName()); int[] indices = getIndices(remove); if (indices != null) { result.getIndices().addAll(Ints.asList(indices)); } if (remove.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) remove.getFeature()).getEAttributeType(); collectCommandValues(remove.getCollection(), dataType, result); } else { collectCommandEObjects(remove.getCollection(), result, false); } } else if (command instanceof SetCommand) { SetCommand set = (SetCommand) command; result = CCommandFactory.eINSTANCE.createCommand(); result.setType(CommandKind.SET); result.setOwner(set.getOwner()); result.setFeature(set.getFeature().getName()); result.getIndices().add(set.getIndex()); if (set.getFeature() instanceof EAttribute) { EDataType dataType = ((EAttribute) set.getFeature()).getEAttributeType(); collectCommandValues(singleton(set.getValue()), dataType, result); } else { collectCommandEObjects(singleton(set.getValue()), result, true); } } else if (command instanceof ReplaceCommand) { throw new EncodingException("todo"); } else if (command instanceof MoveCommand) { throw new EncodingException("todo"); } else { throw new EncodingException("unsupported command type: " + command.getClass().getName()); } 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);  }