/*
 * Copyright 2020 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.android.gnd.ui.common;

import android.content.Context;
import androidx.annotation.Nullable;
import com.google.android.gnd.R;
import com.google.android.gnd.model.feature.Feature;
import java8.util.Optional;

public class FeatureHelper {

  public static String getTitle(@Nullable Optional<Feature> feature) {
    if (feature == null || !feature.isPresent()) {
      return "";
    }
    String caption = feature.get().getCaption();
    String layerName = feature.get().getLayer().getName();
    return caption == null || caption.isEmpty() ? layerName : caption;
  }

  public static String getCreatedBy(Context context, @Nullable Optional<Feature> feature) {
    if (feature == null || !feature.isPresent()) {
      return "";
    }
    String username = feature.get().getCreated().getUser().getDisplayName();
    return String.format(context.getResources().getString(R.string.added_by), username);
  }
}

@@ -692,23 +692,22 @@
 tation.N
+onN
 ull
-able
 ;%0Aimport
@@ -773,298 +773,627 @@
 del.
-feature.Feature;%0Aimport java8.util.Optional;%0A%0Apublic class FeatureHelper %7B%0A%0A  public static String getTitle(@Nullable Optional%3CFeature%3E feature) %7B%0A    if (feature == null %7C%7C !feature.isPresent()) %7B%0A      return 
+AuditInfo;%0Aimport com.google.android.gnd.model.User;%0Aimport com.google.android.gnd.model.feature.Feature;%0Aimport com.google.android.gnd.model.layer.Layer;%0Aimport java8.util.Optional;%0A%0A/** Helper class for converting %7B@link Feature%7D to string for UI components. */%0Apublic class FeatureHelper %7B%0A%0A  public static String getCreatedBy(Context context, @NonNull Optional%3CFeature%3E feature) %7B%0A    return getUserName(feature).map(name -%3E context.getString(R.string.added_by, name)).orElse(
 %22%22
+)
 ;%0A
-  
   %7D%0A
+%0A
   
-  String caption = feature.get().getCaption();%0A    String l
+public static String getTitle(@NonNull Optional%3CFeature%3E feature) %7B%0A    return getCaption(feature).orElseGet(() -%3E getL
 ayerName
  = f
@@ -1392,116 +1392,213 @@
 Name
- = 
+(
 feature
-.get().getLayer().getName();%0A    return caption == null %7C%7C caption.isEmpty() ? 
+).orElse(%22%22));%0A  %7D%0A%0A  private static Optional%3CString%3E getUserName(@NonNull Optional%3CFeature%3E feature) %7B%0A    return feature.map(Feature::getCreated).map(AuditInfo::getUser).map(User::getDisp
 lay
-er
 Name
- : caption
+)
 ;%0A  
@@ -1599,37 +1599,38 @@
 e);%0A  %7D%0A%0A  p
-ublic
+rivate
  static 
 String getCr
@@ -1621,318 +1621,322 @@
 tic 
+Optional%3C
 String
+%3E
  getC
-reatedBy(Context context, @Nullable Optional%3CFeature%3E feature) %7B%0A    if (feature == null %7C%7C !feature.isPresent()) %7B%0A      return %22%22;%0A    %7D%0A    String username = feature.get().getCreated().getUser().getDisplayName();%0A    return String.format(context.getResources().getString(R.string.added_by), usern
+aption(@NonNull Optional%3CFeature%3E feature) %7B%0A    return feature.map(Feature::getCaption).map(String::trim).filter(caption -%3E !caption.isEmpty());%0A  %7D%0A%0A  private static Optional%3CString%3E getLayerName(@NonNull Optional%3CFeature%3E feature) %7B%0A    return feature.map(Feature::getLayer).map(Layer::getN
 ame)

Refactor FeatureHelper (thanks Gino!)