/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.jetbrains.idea.svn;

import com.intellij.idea.RareLogger;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.util.containers.ContainerUtil;
import org.apache.log4j.Level;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tmatesoft.svn.core.SVNErrorCode;
import org.tmatesoft.svn.core.SVNException;

import java.util.Set;

/**
 * @author Konstantin Kolosovsky.
 */
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public class SvnExceptionLogFilter implements RareLogger.LogFilter {

  private static final int ourLogUsualInterval = 20 * 1000;
  private static final int ourLogRareInterval = 30 * 1000;

  private static final Set<SVNErrorCode> ourLogRarelyCodes = ContainerUtil
    .newHashSet(SVNErrorCode.WC_UNSUPPORTED_FORMAT, SVNErrorCode.WC_CORRUPT, SVNErrorCode.WC_CORRUPT_TEXT_BASE, SVNErrorCode.WC_NOT_FILE,
                SVNErrorCode.WC_NOT_DIRECTORY, SVNErrorCode.WC_PATH_NOT_FOUND);

  @Override
  public Object getKey(@NotNull Level level, @NonNls String message, @Nullable Throwable t, @NonNls String... details) {
    SVNException e = getSvnException(t);
    boolean shouldFilter = e != null && ourLogRarelyCodes.contains(e.getErrorMessage().getErrorCode());

    return shouldFilter ? e.getErrorMessage().getErrorCode() : null;
  }

  @Override
  @NotNull
  public Integer getAllowedLoggingInterval(Level level, String message, Throwable t, String[] details) {
    SVNException e = getSvnException(t);
    boolean shouldFilter = e != null && ourLogRarelyCodes.contains(e.getErrorMessage().getErrorCode());

    return shouldFilter ? ourLogRareInterval : ourLogUsualInterval;
  }

  @Nullable
  private static SVNException getSvnException(@Nullable Throwable t) {
    SVNException result = null;
    if (t instanceof SVNException) {
      result = (SVNException)t;
    }
    else if (t instanceof VcsException && t.getCause() instanceof SVNException) {
      result = (SVNException)t.getCause();
    }
    return result;
  }
}

@@ -668,54 +668,8 @@
 er;%0A
-import com.intellij.openapi.vcs.VcsException;%0A
 impo
@@ -887,39 +887,55 @@
 org.
-tmatesoft.svn.core.SVNErrorCode
+jetbrains.idea.svn.commandLine.SvnBindException
 ;%0Aim
@@ -966,24 +966,24 @@
 ore.SVNE
-xception
+rrorCode
 ;%0A%0Aimpor
@@ -1004,105 +1004,189 @@
 t;%0A%0A
-/**%0A * @author Kon
+import 
 sta
-n
 ti
-n Kolosovsky.%0A */%0A@SuppressWarnings(%22ThrowableResultOfMethodCallIgnored%22)
+c com.intellij.util.ObjectUtils.tryCast;%0Aimport static com.intellij.util.containers.ContainerUtil.exists;%0Aimport static com.intellij.util.containers.ContainerUtil.find;%0A
 %0Apub
@@ -1796,34 +1796,38 @@
 details) %7B%0A    S
-VN
+vnBind
 Exception e = ge
@@ -1816,38 +1816,50 @@
 ndException e = 
-getSvn
+tryCast(t, SvnBind
 Exception(t);%0A  
@@ -1855,39 +1855,28 @@
 tion
-(t
+.class
 );%0A
+%0A
     
-boolean shouldFilter =
+return
  e !
@@ -1874,35 +1874,39 @@
 eturn e != null 
-&& 
+? find(
 ourLogRarelyCode
@@ -1902,33 +1902,37 @@
 gRarelyCodes
-.
+, e::
 contains
 (e.getErrorM
@@ -1923,106 +1923,8 @@
 ains
-(e.getErrorMessage().getErrorCode());%0A%0A    return shouldFilter ? e.getErrorMessage().getErrorCode(
 ) : 
@@ -2059,34 +2059,38 @@
 details) %7B%0A    S
-VN
+vnBind
 Exception e = ge
@@ -2087,22 +2087,34 @@
 ion e = 
-getSvn
+tryCast(t, SvnBind
 Exceptio
@@ -2118,39 +2118,28 @@
 tion
-(t
+.class
 );%0A
+%0A
     
-boolean shouldFilter =
+return
  e !
@@ -2148,16 +2148,23 @@
 null && 
+exists(
 ourLogRa
@@ -2176,17 +2176,21 @@
 odes
-.
+, e::
 contains
 (e.g
@@ -2189,458 +2189,52 @@
 ains
-(e.getErrorMessage().getErrorCode());%0A%0A    return shouldFilter ? ourLogRareInterval : ourLogUsualInterval;%0A  %7D%0A%0A  @Nullable%0A  private static SVNException getSvnException(@Nullable Throwable t) %7B%0A    SVNException result = null;%0A    if (t instanceof SVNException) %7B%0A      result = (SVNException)t;%0A    %7D%0A    else if (t instanceof VcsException && t.getCause() instanceof SVNException) %7B%0A      result = (SVNException)t.getCause();%0A    %7D%0A    return result
+) ? ourLogRareInterval : ourLogUsualInterval
 ;%0A  

Fix svn error codes checking in "SvnExceptionLogFilter"