View Javadoc
1   /*
2    * Metis: Java Data Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.metis.viewer;
18  
19  import io.github.tonywasher.joceanus.oceanus.decimal.OceanusDecimal;
20  import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21  import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile;
22  import io.github.tonywasher.joceanus.oceanus.profile.OceanusProfile.OceanusProfileStatus;
23  import io.github.tonywasher.joceanus.metis.data.MetisDataResource;
24  import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
25  import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
26  
27  import java.util.Iterator;
28  
29  /**
30   * Profile wrapper.
31   */
32  public class MetisViewerProfileWrapper
33          implements MetisFieldItem {
34      /**
35       * Local Report fields.
36       */
37      private static final MetisFieldSet<MetisViewerProfileWrapper> FIELD_DEFS = MetisFieldSet.newFieldSet(MetisViewerProfileWrapper.class);
38  
39      /*
40       * Declare Fields.
41       */
42      static {
43          FIELD_DEFS.declareLocalField(MetisDataResource.PROFILE_TASK, MetisViewerProfileWrapper::getName);
44          FIELD_DEFS.declareLocalField(MetisDataResource.PROFILE_STATUS, MetisViewerProfileWrapper::getStatus);
45          FIELD_DEFS.declareLocalField(MetisDataResource.PROFILE_ELAPSED, MetisViewerProfileWrapper::getElapsed);
46          FIELD_DEFS.declareLocalField(MetisDataResource.PROFILE_HIDDEN, MetisViewerProfileWrapper::getHidden);
47      }
48  
49      /**
50       * Report fields.
51       */
52      private final MetisFieldSet<MetisViewerProfileWrapper> theFields = MetisFieldSet.newFieldSet(this);
53  
54      /**
55       * The wrapped profile.
56       */
57      private final OceanusProfile theWrapped;
58  
59      /**
60       * Create a new Metis Profile Wrapper for an underlying TethysProfile.
61       *
62       * @param pProfile the underlying profile
63       */
64      public MetisViewerProfileWrapper(final OceanusProfile pProfile) {
65          /* Store details */
66          theWrapped = pProfile;
67  
68          /* Loop through the subtasks */
69          final Iterator<OceanusProfile> myIterator = theWrapped.subTaskIterator();
70          while (myIterator.hasNext()) {
71              final OceanusProfile mySubTask = myIterator.next();
72              theFields.declareLocalField(mySubTask.getName(), p -> mySubTask);
73          }
74      }
75  
76      @Override
77      public MetisFieldSet<MetisViewerProfileWrapper> getDataFieldSet() {
78          return theFields;
79      }
80  
81      @Override
82      public String formatObject(final OceanusDataFormatter pFormatter) {
83          /* Format the profile */
84          return getName()
85                  + ": "
86                  + (theWrapped.isRunning()
87                  ? getStatus()
88                  : getElapsed());
89      }
90  
91      /**
92       * Obtain the name of the profile.
93       *
94       * @return the name
95       */
96      private String getName() {
97          return theWrapped.getName();
98      }
99  
100     /**
101      * Obtain the status of the profile.
102      *
103      * @return the status
104      */
105     private OceanusProfileStatus getStatus() {
106         return theWrapped.isRunning()
107                 ? theWrapped.getStatus()
108                 : null;
109     }
110 
111     /**
112      * Obtain the elapsed time of the profile.
113      *
114      * @return the elapsedTime
115      */
116     private OceanusDecimal getElapsed() {
117         return theWrapped.isRunning()
118                 ? null
119                 : theWrapped.getElapsed();
120     }
121 
122     /**
123      * Obtain the hidden time of the profile.
124      *
125      * @return the hiddenTime
126      */
127     private OceanusDecimal getHidden() {
128         return theWrapped.getHidden();
129     }
130 }