# Creating a Java archive for the user-defined indicator

Available in...

* Big Data Platform
* Cloud API Services Platform
* Cloud Big Data Platform
* Cloud Data Fabric
* Cloud Data Management Platform
* Data Fabric
* Data Management Platform
* Data Services Platform
* MDM Platform
* Real-Time Big Data Platform

Before creating a Java archive for the user defined indicator, you must define, in Eclipse, the target platform against which the workspace plugins will be compiled and tested.

## Before you begin

You have selected the Profiling perspective.

## Procedure

1. Define the target platform:
   1. In Eclipse, select Preferences to display the Preferences window.
   2. Expand Plug-in Development, select Target Platform and click Add... to open a view where you can create the target definition.
   3. Select the Nothing: Start with an empty target definition option and click Next.
   4. In the Name field, enter a name for the new target definition and click Next.
   5. Select Installation from the Add Content list and click Next.
   6. Use the Browse... button to set the path of the installation directory and click Next.

      ![Location of the Browse... button.](https://help.qlik.com/talend/en-US/studio-user-guide/Resources/images/Java_target_platform5.png)

      The new target definition is displayed in the location list.

      The new target definition is displayed in the location list. ![The new target definition is listed in the Locations tab.](https://help.qlik.com/talend/en-US/studio-user-guide/Resources/images/Java_target_platform6.png)
   7. Click Finish to close the dialog box.
2. Create a Java archive for the user defined indicator
   1. In Eclipse, check out the [project from Git](https://github.com/Talend/tdq-studio-se/tree/master/sample/test.myudi).

      In this Java project, you can find four Java classes that correspond to the four indicator categories listed in the Indicator Category view in the indicator editor.

      In this Java project, you can find four Java classes that correspond to the four indicator categories listed in the Indicator CategoryIndicator Category view in the indicator editor. ![Example of the four Java classes from the Java project.](https://help.qlik.com/talend/en-US/studio-user-guide/Resources/images/Java_Classes.png)

      Each one of these Java classes extends the UserDefIndicatorImpl indicator. The figure below illustrates an example using the MyAvgLength Java class.

      Each one of these Java classes extends the UserDefIndicatorImplUserDefIndicatorImpl indicator. The figure below illustrates an example using the MyAvgLengthMyAvgLength Java class.
      ```
      package test.udi;

      import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl;

      /**
       * @author mzhao
       * 
       * A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined
       * real value. It implements the minimum number of required methods.
       */
      public class MyAvgLength extends UserDefIndicatorImpl {

          private double length = 0;

          @Override
          public boolean reset() {
              super.reset();
              length = 0;
              return true;
          }

          @Override
          public boolean handle(Object data) {
              super.handle(data);
              // an indicator which computes the average text length on data which are more than 2 characters (this means that
              // text values with less than 2 characters are not taken into account).
              int dataLength = (data != null) ? data.toString().length() : 0;
              if (dataLength > 2) {
                  length += dataLength;
              }
              return true;
          }

          /*
           * (non-Javadoc)
           * 
           * @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation()
           */
          @Override
          public boolean finalizeComputation() {
              value = String.valueOf(this.length / (this.getCount() - this.getNullCount()));
              return super.finalizeComputation();
          }

      }
      ```

      `package test.udi;

      import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl;

      /**
       * @author mzhao
       * 
       * A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined
       * real value. It implements the minimum number of required methods.
       */
      public class MyAvgLength extends UserDefIndicatorImpl {

          private double length = 0;

          @Override
          public boolean reset() {
              super.reset();
              length = 0;
              return true;
          }

          @Override
          public boolean handle(Object data) {
              super.handle(data);
              // an indicator which computes the average text length on data which are more than 2 characters (this means that
              // text values with less than 2 characters are not taken into account).
              int dataLength = (data != null) ? data.toString().length() : 0;
              if (dataLength > 2) {
                  length += dataLength;
              }
              return true;
          }

          /*
           * (non-Javadoc)
           * 
           * @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation()
           */
          @Override
          public boolean finalizeComputation() {
              value = String.valueOf(this.length / (this.getCount() - this.getNullCount()));
              return super.finalizeComputation();
          }

      }`
      ```
      package test.udi;

      import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl;

      /**
       * @author mzhao
       * 
       * A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined
       * real value. It implements the minimum number of required methods.
       */
      public class MyAvgLength extends UserDefIndicatorImpl {

          private double length = 0;

          @Override
          public boolean reset() {
              super.reset();
              length = 0;
              return true;
          }

          @Override
          public boolean handle(Object data) {
              super.handle(data);
              // an indicator which computes the average text length on data which are more than 2 characters (this means that
              // text values with less than 2 characters are not taken into account).
              int dataLength = (data != null) ? data.toString().length() : 0;
              if (dataLength > 2) {
                  length += dataLength;
              }
              return true;
          }

          /*
           * (non-Javadoc)
           * 
           * @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation()
           */
          @Override
          public boolean finalizeComputation() {
              value = String.valueOf(this.length / (this.getCount() - this.getNullCount()));
              return super.finalizeComputation();
          }

      }
      ```

      `package test.udi;

      import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl;

      /**
       * @author mzhao
       * 
       * A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined
       * real value. It implements the minimum number of required methods.
       */
      public class MyAvgLength extends UserDefIndicatorImpl {

          private double length = 0;

          @Override
          public boolean reset() {
              super.reset();
              length = 0;
              return true;
          }

          @Override
          public boolean handle(Object data) {
              super.handle(data);
              // an indicator which computes the average text length on data which are more than 2 characters (this means that
              // text values with less than 2 characters are not taken into account).
              int dataLength = (data != null) ? data.toString().length() : 0;
              if (dataLength > 2) {
                  length += dataLength;
              }
              return true;
          }

          /*
           * (non-Javadoc)
           * 
           * @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation()
           */
          @Override
          public boolean finalizeComputation() {
              value = String.valueOf(this.length / (this.getCount() - this.getNullCount()));
              return super.finalizeComputation();
          }

      }``package test.udi;

      import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl;

      /**
       * @author mzhao
       * 
       * A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined
       * real value. It implements the minimum number of required methods.
       */
      public class MyAvgLength extends UserDefIndicatorImpl {

          private double length = 0;

          @Override
          public boolean reset() {
              super.reset();
              length = 0;
              return true;
          }

          @Override
          public boolean handle(Object data) {
              super.handle(data);
              // an indicator which computes the average text length on data which are more than 2 characters (this means that
              // text values with less than 2 characters are not taken into account).
              int dataLength = (data != null) ? data.toString().length() : 0;
              if (dataLength > 2) {
                  length += dataLength;
              }
              return true;
          }

          /*
           * (non-Javadoc)
           * 
           * @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation()
           */
          @Override
          public boolean finalizeComputation() {
              value = String.valueOf(this.length / (this.getCount() - this.getNullCount()));
              return super.finalizeComputation();
          }

      }`package test.udi; import org.talend.dataquality.indicators.sql.impl.UserDefIndicatorImpl; /\*\* \* @author mzhao \* \* A very simple example of a java implementation of a user defined indicator. This indicator returns a user defined \* real value. It implements the minimum number of required methods. \*/ public class MyAvgLength extends UserDefIndicatorImpl { private double length = 0; @Override public boolean reset() { super.reset(); length = 0; return true; } @Override public boolean handle(Object data) { super.handle(data); // an indicator which computes the average text length on data which are more than 2 characters (this means that // text values with less than 2 characters are not taken into account). int dataLength = (data != null) ? data.toString().length() : 0; if (dataLength &gt; 2) { length += dataLength; } return true; } /\* \* (non-Javadoc) \* \* @see org.talend.dataquality.indicators.impl.IndicatorImpl#finalizeComputation() \*/ @Override public boolean finalizeComputation() { value = String.valueOf(this.length / (this.getCount() - this.getNullCount())); return super.finalizeComputation(); } }
   2. Modify the code of the methods that follow each @Override according to your needs.
   3. Optional: Use the following methods in your code to retrieve the indicator parameters:
      | Method | Description |
      | --- | --- |
      | Indicator.getParameter() | Returns an IndicatorParameters<br>object |
      | IndicatorParameters.getIndicatorValidDomain() | Returns a Domain object |
      | Domain.getJavaUDIIndicatorParameter() | Returns a list of JavaUDIIndicatorParameter<br>that stores each key/value pair that defines the<br>parameter |
   4. Save your modifications.
   5. Using Eclipse, export this new Java archive.

## Results

The Java archive is now ready to be attached to any Java indicator you want to create in the Profiling perspective.