private static Set<ExtTrustRelationship> entitiesToExtRelationships(
      final Set<ITrustedEntity> entities, final TrustQuery query) {

    final Set<ExtTrustRelationship> result =
        new LinkedHashSet<ExtTrustRelationship>(entities.size());

    final TrustValueType trustValueType = query.getTrustValueType();
    for (final ITrustedEntity entity : entities) {

      final Set<TrustEvidence> evidenceSet = iEvidenceToEvidence(entity.getEvidence());
      // TODO Needs optimisation
      final Set<TrustEvidence> directEvidenceSet = new LinkedHashSet<TrustEvidence>();
      final Set<TrustEvidence> indirectEvidenceSet = new LinkedHashSet<TrustEvidence>();
      for (final TrustEvidence evidence : evidenceSet) {
        if (evidence.getSourceId() == null) {
          directEvidenceSet.add(evidence);
        } else {
          indirectEvidenceSet.add(evidence);
        }
      }

      if (entity.getDirectTrust().getValue() != null
          && (null == trustValueType || TrustValueType.DIRECT == trustValueType)) {
        result.add(
            new ExtTrustRelationship(
                entity.getTrustorId(),
                entity.getTrusteeId(),
                TrustValueType.DIRECT,
                entity.getDirectTrust().getValue(),
                entity.getDirectTrust().getLastUpdated(),
                directEvidenceSet));
      }
      if (entity.getIndirectTrust().getValue() != null
          && (null == trustValueType || TrustValueType.INDIRECT == trustValueType)) {
        result.add(
            new ExtTrustRelationship(
                entity.getTrustorId(),
                entity.getTrusteeId(),
                TrustValueType.INDIRECT,
                entity.getIndirectTrust().getValue(),
                entity.getIndirectTrust().getLastUpdated(),
                indirectEvidenceSet));
      }
      if (entity.getUserPerceivedTrust().getValue() != null
          && (null == trustValueType || TrustValueType.USER_PERCEIVED == trustValueType)) {
        result.add(
            new ExtTrustRelationship(
                entity.getTrustorId(),
                entity.getTrusteeId(),
                TrustValueType.USER_PERCEIVED,
                entity.getUserPerceivedTrust().getValue(),
                entity.getUserPerceivedTrust().getLastUpdated(),
                new HashSet<TrustEvidence>()));
      }
    }

    return result;
  }
  private static Set<TrustRelationship> entitiesToRelationships(
      final Set<ITrustedEntity> entities, final TrustQuery query) {

    final Set<TrustRelationship> result = new LinkedHashSet<TrustRelationship>();

    final TrustValueType trustValueType = query.getTrustValueType();
    for (final ITrustedEntity entity : entities) {

      if (entity.getDirectTrust().getValue() != null
          && (null == trustValueType || TrustValueType.DIRECT == trustValueType)) {
        result.add(
            new TrustRelationship(
                entity.getTrustorId(),
                entity.getTrusteeId(),
                TrustValueType.DIRECT,
                entity.getDirectTrust().getValue(),
                entity.getDirectTrust().getLastUpdated()));
      }
      if (entity.getIndirectTrust().getValue() != null
          && (null == trustValueType || TrustValueType.INDIRECT == trustValueType)) {
        result.add(
            new TrustRelationship(
                entity.getTrustorId(),
                entity.getTrusteeId(),
                TrustValueType.INDIRECT,
                entity.getIndirectTrust().getValue(),
                entity.getIndirectTrust().getLastUpdated()));
      }
      if (entity.getUserPerceivedTrust().getValue() != null
          && (null == trustValueType || TrustValueType.USER_PERCEIVED == trustValueType)) {
        result.add(
            new TrustRelationship(
                entity.getTrustorId(),
                entity.getTrusteeId(),
                TrustValueType.USER_PERCEIVED,
                entity.getUserPerceivedTrust().getValue(),
                entity.getUserPerceivedTrust().getLastUpdated()));
      }
    }

    return result;
  }
  private Double retrieveLocalTrustValue(final Requestor requestor, final TrustQuery query)
      throws TrustException {

    final Set<ITrustedEntity> entities = this.retrieveTrustedEntities(query);

    if (entities.isEmpty()) {
      return null;
    } else if (entities.size() == 1) {
      final ITrustedEntity entity = entities.iterator().next();
      if (TrustValueType.DIRECT == query.getTrustValueType()) {
        return entity.getDirectTrust().getValue();
      } else if (TrustValueType.INDIRECT == query.getTrustValueType()) {
        return entity.getIndirectTrust().getValue();
      } else { // if (TrustValueType.USER_PERCEIVED == query.getTrustValueType())
        return entity.getUserPerceivedTrust().getValue();
      }
    } else {
      throw new NonUniqueTrustQueryResultException(
          "Query returned " + entities.size() + " results");
    }
  }