简体   繁体   中英

Java check if object is instanceof Object

I have made a function where I need to show my history entries for an object. For example i have 5 Objects and each of them has 5 Entries which would give me 25 entries back.

Now I need to increment each entry for the Object, which would return me for every object the amount of entries which are inside the object.

The function should be working except of this part.

    if(entries instanceof history_entry){
        return Historyindex++;
    }

the whole function

  public static Value getMeasurementHistoryIndex(Session session, Expression expr_id, Expression expr_history_id) {
//if (!(columnExpression instanceof ExpressionColumn)) throw new IllegalArgumentException("column expression expected in localized(<expression>,<locale>)");
Value value_id = expr_id.getValue(session);
Value value_history_id = expr_history_id.getValue(session);

Extent extent = getExtent(session);
if(extent == null || value_id instanceof ValueNull || value_history_id instanceof ValueNull) {
  return ValueNull.INSTANCE;
}

ModelObject obj = null;
Measurement2 measurement = null;
MeasurementHistoryEntry history_entry = null;
History_index Historyindex = null;

if (value_id instanceof ValueString) {
  try {
      obj = extent.get(GUID.parse(value_id.getString()));
  } catch(Exception e) {
      Log.log(Log.DEBUG, "Function GET_MEASUREMENT_HISTORY_INDEX: could not parse measurement ID: " + value_id.getString());
  }
}

if(obj instanceof Measurement2) {
    measurement = (Measurement2)obj;
}

if (value_history_id instanceof ValueString) {
    try {
        obj = extent.get(GUID.parse(value_history_id.getString()));
    } catch(Exception e) {
      Log.log(Log.DEBUG, "Function GET_MEASUREMENT_HISTORY_INDEX: could not parse measurement ID: " + value_id.getString());
    }
  }

if(obj instanceof MeasurementHistoryEntry) {
    history_entry = (MeasurementHistoryEntry)obj;
}

if(measurement == null || history_entry == null) {
    return ValueNull.INSTANCE;
}


ListView history = HistorizableHelper.createDateSortedHistoryListView(MeasurementHistoryEntry.TYPE);
history.setModel(measurement);
ModelObject[] entries = history.getObjects();
history.setModel(entries);


if(entries instanceof history_entry)
{
    return Historyindex++;

}


return ValueNull.INSTANCE;

}

So at the end I should have for every object a number which returns the amount of entries inside the objects.

I hope someone can help me solve that if statement or maybe i need to do a for loop.

Thanks in advance.

Greets Adem

To check all the history entries in multiple histories i used this code except of the if statement above.

    for (int i = 0; i < entries.length; i++) {
    ModelObject entry = entries[i];
    if(history_entry.equals(entry))
    return ValueInt.get(i);

this helped me increment from 1 to x for every entry

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM