blob: a59ddf5a936765af798d227a5ff92466d0e32afb [file] [log] [blame]
jaegonkime0f45b52018-10-09 20:23:26 +09001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.workflow.api;
17
18import java.util.Objects;
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21
22/**
23 * An interface representing workflow program counter.
24 */
25public final class ProgramCounter {
26
27 public static final ProgramCounter INIT_PC = ProgramCounter.valueOf(Worklet.Common.INIT.name(), 0);
28
29 /**
30 * index of the worklet.
31 */
jaegonkim2baae8c2019-05-16 14:40:38 +090032 private final int workletIndex;
jaegonkime0f45b52018-10-09 20:23:26 +090033
34 /**
35 * Type of worklet.
36 */
jaegonkim2baae8c2019-05-16 14:40:38 +090037 private final String workletType;
jaegonkime0f45b52018-10-09 20:23:26 +090038
39 /**
40 * Index of worklet.
41 * @return index of worklet
42 */
43 public int workletIndex() {
44 return this.workletIndex;
45 }
46
47 /**
48 * Type of worklet.
49 * @return type of worklet
50 */
51 public String workletType() {
52 return this.workletType;
53 }
54
55 /**
56 * Constructor of workflow Program Counter.
57 * @param workletType type of worklet
58 * @param workletIndex index of worklet
59 */
60 private ProgramCounter(String workletType, int workletIndex) {
61 this.workletType = workletType;
62 this.workletIndex = workletIndex;
63 }
64
jaegonkimf85ee3c2019-04-21 11:10:25 +090065 /**
66 * Clones this workflow Program Counter.
67 * @return clone of this workflow Program Counter
68 */
69 public ProgramCounter clone() {
70 return ProgramCounter.valueOf(this.workletType(), this.workletIndex());
71 }
72
73 /**
74 * Returns whether this program counter is INIT worklet program counter.
75 * @return whether this program counter is INIT worklet program counter
76 */
77 public boolean isInit() {
78 return Worklet.Common.INIT.tag().equals(this.workletType);
79 }
80
81 /**
82 * Returns whether this program counter is COMPLETED worklet program counter.
83 * @return whether this program counter is COMPLETED worklet program counter
84 */
85 public boolean isCompleted() {
86 return Worklet.Common.COMPLETED.tag().equals(this.workletType);
87 }
88
jaegonkime0f45b52018-10-09 20:23:26 +090089 @Override
90 public int hashCode() {
91 return Objects.hash(this.toString());
92 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (obj == this) {
97 return true;
98 }
99 if (!(obj instanceof ProgramCounter)) {
100 return false;
101 }
102 return Objects.equals(this.workletType(), ((ProgramCounter) obj).workletType())
103 && Objects.equals(this.workletIndex(), ((ProgramCounter) obj).workletIndex());
104 }
105
106 @Override
107 public String toString() {
108 return String.format("(%d)%s", workletIndex, workletType);
109 }
110
111 /**
112 * Builder of workflow Program Counter.
113 * @param workletType type of worklet
114 * @param workletIndex index of worklet
115 * @return program counter
116 */
117 public static ProgramCounter valueOf(String workletType, int workletIndex) {
118 return new ProgramCounter(workletType, workletIndex);
119 }
120
121 /**
122 * Builder of workflow Program Counter.
123 * @param strProgramCounter string format for program counter
124 * @return program counter
125 */
126 public static ProgramCounter valueOf(String strProgramCounter) {
127
128 Matcher m = Pattern.compile("\\((\\d+)\\)(.+)").matcher(strProgramCounter);
129
130 if (!m.matches()) {
131 throw new IllegalArgumentException("Malformed program counter string");
132 }
133
134 return new ProgramCounter(m.group(2), Integer.parseInt(m.group(1)));
135 }
136
137}
138