-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,718 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
phive-result/src/main/java/com/helger/phive/result/PhiveResultHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* | ||
* Copyright (C) 2014-2024 Philip Helger (www.helger.com) | ||
* philip[at]helger[dot]com | ||
* | ||
* 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 com.helger.phive.result; | ||
|
||
import java.net.MalformedURLException; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
import com.helger.commons.ValueEnforcer; | ||
import com.helger.commons.annotation.Nonempty; | ||
import com.helger.commons.error.level.EErrorLevel; | ||
import com.helger.commons.error.level.IErrorLevel; | ||
import com.helger.commons.io.resource.ClassPathResource; | ||
import com.helger.commons.io.resource.FileSystemResource; | ||
import com.helger.commons.io.resource.IReadableResource; | ||
import com.helger.commons.io.resource.URLResource; | ||
import com.helger.commons.io.resource.inmemory.IMemoryReadableResource; | ||
import com.helger.commons.io.resource.wrapped.IWrappedReadableResource; | ||
import com.helger.commons.state.ETriState; | ||
import com.helger.commons.string.StringHelper; | ||
|
||
/** | ||
* Contains stateless phive result helper methods. | ||
* | ||
* @author Philip Helger | ||
*/ | ||
@Immutable | ||
public final class PhiveResultHelper | ||
{ | ||
public static final String VALUE_ERRORLEVEL_SUCCESS = "SUCCESS"; | ||
public static final String VALUE_ERRORLEVEL_WARN = "WARN"; | ||
public static final String VALUE_ERRORLEVEL_ERROR = "ERROR"; | ||
|
||
public static final String VALUE_TRISTATE_TRUE = "TRUE"; | ||
public static final String VALUE_TRISTATE_FALSE = "FALSE"; | ||
public static final String VALUE_TRISTATE_UNDEFINED = "UNDEFINED"; | ||
|
||
private PhiveResultHelper () | ||
{} | ||
|
||
public static boolean isConsideredError (@Nonnull final IErrorLevel aErrorLevel) | ||
{ | ||
return aErrorLevel.isGE (EErrorLevel.ERROR); | ||
} | ||
|
||
public static boolean isConsideredWarning (@Nonnull final IErrorLevel aErrorLevel) | ||
{ | ||
return aErrorLevel.isGE (EErrorLevel.WARN); | ||
} | ||
|
||
/** | ||
* Get the string of the error level. One of <code>"ERROR"</code>, | ||
* <code>"WARN"</code> or <code>"SUCCESS"</code>.<br> | ||
* See {@link #VALUE_ERRORLEVEL_SUCCESS}, {@link #VALUE_ERRORLEVEL_WARN}, | ||
* {@link #VALUE_ERRORLEVEL_ERROR} | ||
* | ||
* @param aErrorLevel | ||
* The error level to convert. May not be <code>null</code>. | ||
* @return A non-<code>null</code> value string. | ||
*/ | ||
@Nonnull | ||
@Nonempty | ||
public static String getErrorLevelValue (@Nonnull final IErrorLevel aErrorLevel) | ||
{ | ||
ValueEnforcer.notNull (aErrorLevel, "ErrorLevel"); | ||
|
||
if (PhiveResultHelper.isConsideredError (aErrorLevel)) | ||
return VALUE_ERRORLEVEL_ERROR; | ||
if (PhiveResultHelper.isConsideredWarning (aErrorLevel)) | ||
return VALUE_ERRORLEVEL_WARN; | ||
return VALUE_ERRORLEVEL_SUCCESS; | ||
} | ||
|
||
@Nullable | ||
public static IErrorLevel getAsErrorLevel (@Nullable final String sErrorLevel) | ||
{ | ||
if (VALUE_ERRORLEVEL_ERROR.equals (sErrorLevel)) | ||
return EErrorLevel.ERROR; | ||
if (VALUE_ERRORLEVEL_WARN.equals (sErrorLevel)) | ||
return EErrorLevel.WARN; | ||
if (VALUE_ERRORLEVEL_SUCCESS.equals (sErrorLevel)) | ||
return EErrorLevel.SUCCESS; | ||
return null; | ||
} | ||
|
||
/** | ||
* Get the tri-state representation of the provided value. Either | ||
* {@link #VALUE_TRISTATE_TRUE} or {@link #VALUE_TRISTATE_FALSE}. | ||
* | ||
* @param b | ||
* boolean value to get converted. | ||
* @return A non-<code>null</code> value string. | ||
* @see #getTriStateValue(ETriState) | ||
*/ | ||
@Nonnull | ||
@Nonempty | ||
public static String getTriStateValue (final boolean b) | ||
{ | ||
return b ? VALUE_TRISTATE_TRUE : VALUE_TRISTATE_FALSE; | ||
} | ||
|
||
/** | ||
* Get the tri-state representation of the provided value. Either | ||
* {@link #VALUE_TRISTATE_TRUE}, {@link #VALUE_TRISTATE_FALSE} or | ||
* {@link #VALUE_TRISTATE_UNDEFINED}. | ||
* | ||
* @param eTriState | ||
* Tri-state value to get converted. May not be <code>null</code>. | ||
* @return A non-<code>null</code> value string. | ||
* @see #getTriStateValue(boolean) | ||
*/ | ||
@Nonnull | ||
public static String getTriStateValue (@Nonnull final ETriState eTriState) | ||
{ | ||
ValueEnforcer.notNull (eTriState, "TriState"); | ||
|
||
if (eTriState.isUndefined ()) | ||
return VALUE_TRISTATE_UNDEFINED; | ||
return getTriStateValue (eTriState.isTrue ()); | ||
} | ||
|
||
/** | ||
* Convert the provided value into a tri-state value. Must be one of | ||
* {@link #VALUE_TRISTATE_TRUE}, {@link #VALUE_TRISTATE_FALSE} or | ||
* {@link #VALUE_TRISTATE_UNDEFINED}. | ||
* | ||
* @param sTriState | ||
* Source value. May be <code>null</code>. | ||
* @return <code>null</code> if the provided value is unknown. | ||
*/ | ||
@Nullable | ||
public static ETriState getAsTriState (@Nullable final String sTriState) | ||
{ | ||
if (VALUE_TRISTATE_TRUE.equals (sTriState)) | ||
return ETriState.TRUE; | ||
if (VALUE_TRISTATE_FALSE.equals (sTriState)) | ||
return ETriState.FALSE; | ||
if (VALUE_TRISTATE_UNDEFINED.equals (sTriState)) | ||
return ETriState.UNDEFINED; | ||
return null; | ||
} | ||
|
||
@Nonnull | ||
@Nonempty | ||
public static String getArtifactPathType (@Nonnull final IReadableResource aRes) | ||
{ | ||
if (aRes instanceof ClassPathResource) | ||
return "classpath"; | ||
if (aRes instanceof FileSystemResource) | ||
return "file"; | ||
if (aRes instanceof URLResource) | ||
return "url"; | ||
if (aRes instanceof IMemoryReadableResource) | ||
return "in-memory"; | ||
if (aRes instanceof IWrappedReadableResource) | ||
return "wrapped"; | ||
return "unknown"; | ||
} | ||
|
||
@Nullable | ||
public static IReadableResource getAsValidationResource (@Nullable final String sArtefactPathType, | ||
@Nullable final String sArtefactPath) | ||
{ | ||
if (StringHelper.hasNoText (sArtefactPathType)) | ||
return null; | ||
if (StringHelper.hasNoText (sArtefactPath)) | ||
return null; | ||
|
||
if ("file".equals (sArtefactPathType)) | ||
return new FileSystemResource (sArtefactPath); | ||
|
||
if ("url".equals (sArtefactPathType)) | ||
try | ||
{ | ||
return new URLResource (sArtefactPath); | ||
} | ||
catch (final MalformedURLException ex) | ||
{ | ||
return null; | ||
} | ||
|
||
// Default to class path | ||
return new ClassPathResource (sArtefactPath); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.