/*
Copyright 2012 Software Freedom Conservancy

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

     http://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 org.openqa.selenium.logging;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;

/**
 * Stores and retrieves logs in-process (i.e. without any RPCs).
 */
public class LocalLogs implements Logs {

  /**
   * Logger which doesn't do anything.
   */
  public static final LocalLogs NULL_LOGGER = new LocalLogs() {
    @Override
    public void addEntry(String logType, LogEntry entry) {
    }
  };
  
  private Map<String, List<LogEntry>> localLogs = new HashMap<String, List<LogEntry>>();
  private final Set<String> logTypesToIgnore;

  public LocalLogs() {
    this.logTypesToIgnore = ImmutableSet.<String>of();
  }

  public LocalLogs(Set<String> logTypesToIgnore) {
    this.logTypesToIgnore = logTypesToIgnore;
  }

  public LogEntries get(String logType) {
    return new LogEntries(getLocalLogs(logType));
  }

  private Iterable<LogEntry> getLocalLogs(String logType) {
    if (localLogs.containsKey(logType)) {
      return localLogs.get(logType);
    }
    return Lists.newArrayList();
  }

  public void addEntry(String logType, LogEntry entry) {
    if (logTypesToIgnore.contains(logType)) {
      return;
    }
    if (!localLogs.containsKey(logType)) {
      localLogs.put(logType, Lists.newArrayList(entry));
    } else {
      localLogs.get(logType).add(entry);
    }
  }

  public Set<String> getAvailableLogTypes() {
    return ImmutableSet.<String>of(LogType.CLIENT);
  }
}
@@ -619,95 +619,48 @@
 ort 
-java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.
+com.google.common.collect.Immutable
 Set;%0A
-%0A
 impo
@@ -684,36 +684,29 @@
 mon.collect.
-ImmutableSet
+Lists
 ;%0Aimport com
@@ -728,21 +728,88 @@
 collect.
-Lists
+Maps;%0A%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0A%0A/**%0A 
@@ -1037,22 +1037,8 @@
 ) %7B%0A
-    @Override%0A
     
@@ -1103,18 +1103,16 @@
  %7D%0A  %7D;%0A
-  
 %0A  priva
@@ -1114,16 +1114,22 @@
 private 
+final 
 Map%3CStri
@@ -1164,43 +1164,23 @@
 s = 
+Maps.
 new
- 
 HashMap
-%3CString, List%3CLogEntry%3E%3E
 ();%0A
@@ -1245,37 +1245,32 @@
 calLogs() %7B%0A    
-this.
 logTypesToIgnore
@@ -1285,24 +1285,16 @@
 ableSet.
-%3CString%3E
 of();%0A  
@@ -1436,24 +1436,123 @@
  logType) %7B%0A
+    // TODO(andreastt): Should presumably clear the logs of that type to reduce memory consumption%0A
     return n
@@ -1735,24 +1735,25 @@
 ype);%0A    %7D%0A
+%0A
     return L
@@ -1770,32 +1770,177 @@
 rayList();%0A  %7D%0A%0A
+  /**%0A   * Add a new log entry to the local storage.%0A   *%0A   * @param logType the log type to store%0A   * @param entry   the entry to store%0A   */%0A
   public void ad
@@ -2046,16 +2046,17 @@
 ;%0A    %7D%0A
+%0A
     if (
@@ -2273,50 +2273,30 @@
 urn 
-ImmutableSet.%3CString%3Eof(LogType.CLIENT
+localLogs.keySet(
 );%0A  %7D%0A
+%0A
 %7D

Return list of available log types in this instance of LocalLogs