All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.weaverplatform.util.SortUtil Maven / Gradle / Ivy

There is a newer version: 3.27.0
Show newest version
package com.weaverplatform.util;

import com.weaverplatform.protocol.model.CreateAttributeOperation;
import com.weaverplatform.protocol.model.CreateRelationOperation;
import com.weaverplatform.protocol.model.WriteOperation;

import java.util.Comparator;

/**
 * @author bastbijl, Sysunite 2018
 */
public class SortUtil {

  public static String keyFilter(String key) {
    if(key.equals("rdf:type")) {
      return "a";
    }
    return key;
  }

  public static String sortKey(WriteOperation operation) {
    if(operation instanceof CreateAttributeOperation) {
      CreateAttributeOperation op = (CreateAttributeOperation) operation;
      return op.getId() + keyFilter(op.getKey()) + op.getSourceId();
    }
    if(operation instanceof CreateRelationOperation) {
      CreateRelationOperation op = (CreateRelationOperation) operation;
      return op.getTargetId() + keyFilter(op.getKey()) + (op.getSourceId());
    }
    return operation.getId();
  }

  public static Comparator charComparator = new Comparator() {
    @Override
    public int compare(Character o1, Character o2) {
      return compareChar(o1, o2);
    }
  };

  public static Comparator writeOperationComparator = new Comparator() {
    @Override
    public int compare(WriteOperation o1, WriteOperation o2) {
      String key1 = sortKey(o1);
      String key2 = sortKey(o2);
      return compareReverseIndex(key1, key2, 0);
    }
  };

  public static int compareReverseIndex(String key1, String key2, int reverseIndex) {
    int i1 = key1.length() - reverseIndex - 1;
    int i2 = key2.length() - reverseIndex - 1;
    if(i1 > -1 && i2 > -1) {
      char c1 = key1.charAt(i1);
      char c2 = key2.charAt(i2);
      if(c1 == c2) {
        return compareReverseIndex(key1, key2, reverseIndex + 1);

      } else {
        return compareChar(c1, c2);
      }
    }
    if(i1 == -1 && i2 == -1) {
      return 0;
    }
    if(i1 == -1) {
      return -1;
    }
    return 1;
  }

  public static int compareChar(char c1, char c2) {
    if(c1 == c2) {
      return 0;

      // Special case for ':'
    } else if(c1 == ':') {
      return -1;
    } else if(c2 == ':') {
      return 1;

    } else {
      return c1 - c2;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy