// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.sps.data;

import com.google.common.collect.ImmutableSet;
import java.util.Optional;
import java.util.Set;

/**
 * Object for holding the analysis results served by ReceiptAnalysisServlet.
 */
public class AnalysisResults {
  private final String rawText;
  private final ImmutableSet<String> categories;
  private final Optional<String> store;
  private final Optional<Long> timestamp;

  public AnalysisResults(
      String rawText, Set<String> categories, Optional<String> store, Optional<Long> timestamp) {
    this.rawText = rawText;
    this.categories = ImmutableSet.copyOf(categories);
    this.store = store;
    this.timestamp = timestamp;
  }

  public String getRawText() {
    return rawText;
  }

  public ImmutableSet<String> getCategories() {
    return categories;
  }

  public Optional<String> getStore() {
    return store;
  }

  public Optional<Long> getTimestamp() {
    return timestamp;
  }

  public static class Builder {
    private final String rawText;
    private ImmutableSet<String> categories;
    private Optional<String> store;
    private Optional<Long> timestamp;

    public Builder(String rawText) {
      this.rawText = rawText;
    }

    public String getRawText() {
      return rawText;
    }

    public Builder setCategories(Set<String> categories) {
      this.categories = ImmutableSet.copyOf(categories);
      return this;
    }

    public Builder setStore(String store) {
      this.store = Optional.of(store);
      return this;
    }

    public Builder setTimestamp(long timestamp) {
      this.timestamp = Optional.of(Long.valueOf(timestamp));
      return this;
    }

    public AnalysisResults build() {
      // No logo was detected.
      if (store == null) {
        store = Optional.empty();
      }

      // No date was detected.
      if (timestamp == null) {
        timestamp = Optional.empty();
      }

      return new AnalysisResults(rawText, categories, store, timestamp);
    }
  }
}

@@ -1698,24 +1698,43 @@
 g%3E timestamp
+ = Optional.empty()
 ;%0A%0A    publi
@@ -2390,117 +2390,8 @@
  %7D%0A%0A
-      // No date was detected.%0A      if (timestamp == null) %7B%0A        timestamp = Optional.empty();%0A      %7D%0A%0A
     

Initialize timestamp in builder