예제 #1
0
  public String getActionForFragment(String fragment) {
    int fragmentVal = validateAndConvertFragment(fragment);
    for (Segment segment : segments) {
      if (segment.valueWithinLimits(fragmentVal)) {
        return segment.getAction();
      }
    }

    throw new RuntimeException(
        "Failed to find action string for valid fragment value \""
            + fragment
            + "\"; this should never happen");
  }
예제 #2
0
  public String putFragment(String fragment) {
    int fragmentVal = validateAndConvertFragment(fragment);

    for (Segment segment : this.segments) {
      if (segment.valueWithinLimits(fragmentVal)) {
        if (!segment.hasMetadataKey(Segment.SIGNIFICANT_BITS_IN_LAST_FRAGMENT_KEY)) {
          String sigBitsKey = keyGenerator.next();
          // Log.d(TAG, "Setting sig-bit metadata key for \"" + segment.getAction() + "\" to \"" +
          // sigBitsKey + "\"");
          segment.setMetadataKey(Segment.SIGNIFICANT_BITS_IN_LAST_FRAGMENT_KEY, sigBitsKey);

          String segmentNumberKey = keyGenerator.next();
          // Log.d(TAG, "Setting segment number metadata key for \"" + segment.getAction() + "\" to
          // \"" + segmentNumberKey + "\"");
          segment.setMetadataKey(Segment.SEGMENT_NUMBER_KEY, segmentNumberKey);

          String segmentCountKey = keyGenerator.next();
          // Log.d(TAG, "Setting segment count metadata key for \"" + segment.getAction() + "\" to
          // \"" + segmentCountKey + "\"");
          segment.setMetadataKey(Segment.MESSAGE_SEGMENT_COUNT_KEY, segmentCountKey);
        }

        String key = keyGenerator.next();

        // TODO: Remove the duplication between these
        segment.addFragment(key, fragment);
        fragmentKeyMap.put(key, fragment);

        // Update the number of significant bits in the last fragment (i.e. the one that was just
        // added)
        int sigBitsInFragment = fragment.length();

        // Log.d(TAG, "Setting significant bits for \"" + segment.getAction() + "\" to " +
        // sigBitsInFragment);

        String sigBitsMetadataFragmentBitstring = Integer.toBinaryString(sigBitsInFragment);
        segment.setMetadataValue(
            Segment.SIGNIFICANT_BITS_IN_LAST_FRAGMENT_KEY, sigBitsMetadataFragmentBitstring);
        return key;
      }
    }

    return null; // This should never happen
  }