public KeysetHandle build() throws GeneralSecurityException {
      if (errorToThrow != null) {
        throw new GeneralSecurityException(
            "Cannot build keyset due to error in original", errorToThrow);
      }
      if (buildCalled) {
        throw new GeneralSecurityException("KeysetHandle.Builder#build must only be called once");
      }
      buildCalled = true;
      Keyset.Builder keysetBuilder = Keyset.newBuilder();
      List<KeysetHandle.Entry> handleEntries = new ArrayList<>(entries.size());
      Integer primaryId = null;

      checkIdAssignments(entries);
      Set<Integer> idsSoFar = new HashSet<>();
      for (KeysetHandle.Builder.Entry builderEntry : entries) {
        if (builderEntry.keyStatus == null) {
          throw new GeneralSecurityException("Key Status not set.");
        }
        int id = getNextIdFromBuilderEntry(builderEntry, idsSoFar);
        if (idsSoFar.contains(id)) {
          throw new GeneralSecurityException("Id " + id + " is used twice in the keyset");
        }
        idsSoFar.add(id);

        Keyset.Key keysetKey;
        @Nullable KeysetHandle.Entry handleEntry;

        if (builderEntry.key != null) {
          handleEntry =
              new KeysetHandle.Entry(
                  builderEntry.key, builderEntry.keyStatus, id, builderEntry.isPrimary);
          keysetKey = createKeysetKey(builderEntry.key, builderEntry.keyStatus, id);
        } else {
          Integer idRequirement = builderEntry.parameters.hasIdRequirement() ? id : null;
          Key key =
              MutableKeyCreationRegistry.globalInstance()
                  .createKey(builderEntry.parameters, idRequirement);
          handleEntry =
              new KeysetHandle.Entry(key, builderEntry.keyStatus, id, builderEntry.isPrimary);
          keysetKey = createKeysetKey(key, builderEntry.keyStatus, id);
        }
        keysetBuilder.addKey(keysetKey);
        if (builderEntry.isPrimary) {
          if (primaryId != null) {
            throw new GeneralSecurityException("Two primaries were set");
          }
          primaryId = id;
          if (builderEntry.keyStatus != KeyStatus.ENABLED) {
            throw new GeneralSecurityException("Primary key is not enabled");
          }
        }
        handleEntries.add(handleEntry);
      }
      if (primaryId == null) {
        throw new GeneralSecurityException("No primary was set");
      }
      keysetBuilder.setPrimaryKeyId(primaryId);
      Keyset keyset = keysetBuilder.build();
      assertEnoughKeyMaterial(keyset);
      return new KeysetHandle(keyset, handleEntries, annotations);
    }
Creates a new {@code KeysetHandle}.

<p>Throws a {@code GeneralSecurityException} if one of the following holds

<ul>
  <li>No entry was marked as primary
  <li>There is an entry in which the ID has not been set and which did not have a predefined
      ID (see {@link Builder.Entry}).
  <li>There is a {@code withRandomId}-entry which is followed by a non {@code
      withRandomId}-entry
  <li>There are two entries with the same {@code withFixedId} (including pre-existing keys
      and imported keys which have an id requirement).
  <li>{@code build()} was previously called for {@code withRandomId} entries, and hence
      calling {@code build()} twice would result in a keyset with different key IDs.
  <li>The primary key is not enabled
</ul>
java_src/src/main/java/com/google/crypto/tink/KeysetHandle.java