001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.bct.assertions;
018
019import static org.apache.juneau.commons.utils.ResourceBundleUtils.*;
020import static org.apache.juneau.commons.utils.Shorts.*;
021
022import java.util.*;
023
024/**
025 * A lightweight message-bundle loader for the assertions classes.
026 *
027 * <p>
028 * Self-contained replacement for the marshall {@code Messages} loader, built on commons
029 * {@link org.apache.juneau.commons.utils.ResourceBundleUtils}.  Supports only the single pattern used by the
030 * assertion classes: {@code Messages.of(SomeAssertion.class, "Messages").getString(shortKey)}.
031 */
032public class Messages {
033
034   private final Class<?> forClass;
035   private final ResourceBundle bundle;
036
037   private Messages(Class<?> forClass, ResourceBundle bundle) {
038      this.forClass = forClass;
039      this.bundle = bundle;
040   }
041
042   /**
043    * Creates a message bundle for the specified class.
044    *
045    * @param forClass The class the bundle belongs to.  Must not be <jk>null</jk>.
046    * @param name The bundle name (e.g. <js>"Messages"</js>).  Must not be <jk>null</jk>.
047    * @return A new message bundle.  Never <jk>null</jk>.
048    */
049   public static Messages of(Class<?> forClass, String name) {
050      reqnn("forClass", forClass);
051      reqnn("name", name);
052      var baseName = forClass.getPackage().getName() + "." + name;
053      var bundle = findBundle(baseName, Locale.getDefault(), forClass.getClassLoader());
054      return new Messages(forClass, bundle);
055   }
056
057   /**
058    * Returns the value for the specified key.
059    *
060    * <p>
061    * Resolves <c>&lt;SimpleClassName&gt;.&lt;key&gt;</c> first, then the bare <c>key</c>.
062    *
063    * @param key The short message key.
064    * @return The resolved value, or <js>"{!key}"</js> if not found.
065    */
066   public String getString(String key) {
067      if (bundle != null) {
068         var fullKey = forClass.getSimpleName() + "." + key;
069         if (bundle.containsKey(fullKey))
070            return bundle.getString(fullKey);
071         if (bundle.containsKey(key))
072            return bundle.getString(key);
073      }
074      return "{!" + key + "}";
075   }
076}