blob: 6af50e0762b80b074cb611ed86ed044ae3c990e3 [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 */
32 private int workletIndex;
33
34 /**
35 * Type of worklet.
36 */
37 private String workletType;
38
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
65 @Override
66 public int hashCode() {
67 return Objects.hash(this.toString());
68 }
69
70 @Override
71 public boolean equals(Object obj) {
72 if (obj == this) {
73 return true;
74 }
75 if (!(obj instanceof ProgramCounter)) {
76 return false;
77 }
78 return Objects.equals(this.workletType(), ((ProgramCounter) obj).workletType())
79 && Objects.equals(this.workletIndex(), ((ProgramCounter) obj).workletIndex());
80 }
81
82 @Override
83 public String toString() {
84 return String.format("(%d)%s", workletIndex, workletType);
85 }
86
87 /**
88 * Builder of workflow Program Counter.
89 * @param workletType type of worklet
90 * @param workletIndex index of worklet
91 * @return program counter
92 */
93 public static ProgramCounter valueOf(String workletType, int workletIndex) {
94 return new ProgramCounter(workletType, workletIndex);
95 }
96
97 /**
98 * Builder of workflow Program Counter.
99 * @param strProgramCounter string format for program counter
100 * @return program counter
101 */
102 public static ProgramCounter valueOf(String strProgramCounter) {
103
104 Matcher m = Pattern.compile("\\((\\d+)\\)(.+)").matcher(strProgramCounter);
105
106 if (!m.matches()) {
107 throw new IllegalArgumentException("Malformed program counter string");
108 }
109
110 return new ProgramCounter(m.group(2), Integer.parseInt(m.group(1)));
111 }
112
113}
114