1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.peaseplate.internal.lang.command;
21
22 import org.peaseplate.TemplateRuntimeException;
23 import org.peaseplate.internal.BuildContext;
24 import org.peaseplate.locator.TemplateLocator;
25
26 public class ComplementCommand extends AbstractUnaryCommand {
27
28 public ComplementCommand(TemplateLocator locator, int line, int column, ICommand command) {
29 super(locator, line, column, command);
30 }
31
32
33
34
35 @Override
36 public String getSign() {
37 return "~";
38 }
39
40
41
42
43 public Object call(BuildContext context) throws TemplateRuntimeException {
44 return complementOf(callCommand(context));
45 }
46
47 protected Object complementOf(Object value) throws TemplateRuntimeException {
48 if (value == null)
49 throw new TemplateRuntimeException(
50 getCommand().getLocator(), getCommand().getLine(), getCommand().getColumn(),
51 "Unexpected NULL value"
52 );
53
54 if (value instanceof Boolean)
55 return Boolean.valueOf(!((Boolean)value).booleanValue());
56 else if (value instanceof Integer)
57 return Integer.valueOf(~((Integer)value).intValue());
58 else if (value instanceof Long)
59 return Long.valueOf(~((Long)value).longValue());
60
61 throw new TemplateRuntimeException(
62 getLocator(), getLine(), getColumn(),
63 "Operation \"~\" (unary) not compatible with \"" + value.getClass() + "\""
64 );
65 }
66 }