/*
 * Copyright 2010 TranceCode
 *
 * 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.trancecode.core;

import java.util.concurrent.TimeUnit;

/**
 * Utility methods related to {@link Thread} and {@link ThreadLocal}.
 * 
 * @author Herve Quiroz
 */
public final class TcThreads
{
    private TcThreads()
    {
        // No instantiation
    }

    public static boolean sleep(final long duration, final TimeUnit timeUnit)
    {
        try
        {
            Thread.sleep(timeUnit.toMillis(duration));
        }
        catch (final InterruptedException e)
        {
            return true;
        }

        return false;
    }

    public static boolean join(final Thread thread)
    {
        try
        {
            thread.join();
        }
        catch (final InterruptedException e)
        {
            return true;
        }

        return false;
    }

    /**
     * Sets the value of a {@link ThreadLocal} and return the previous value.
     */
    public static <T> T set(final ThreadLocal<T> threadLocal, final T value)
    {
        return set(threadLocal, value, null);
    }

    /**
     * Sets the value of a {@link ThreadLocal} and return the previous value, or
     * the specified default value if {@code null}.
     */
    public static <T> T set(final ThreadLocal<T> threadLocal, final T value, final T defaultValue)
    {
        final T oldValue = threadLocal.get();
        threadLocal.set(value);
        if (oldValue == null)
        {
            return defaultValue;
        }

        return oldValue;
    }
}

@@ -35,375 +35,340 @@
 %0A * 
-Licensed under the Apache License, Version 2.0 (the %22License%22); you may not
+This library is free software; you can redistribute it and/or modify it under
 %0A * 
-use this file except in compliance with the License.  You may obtain a copy%0A * of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0
+the terms of the GNU Lesser General Public License as published by the Free%0A * Software Foundation; either version 2.1 of the License, or (at your option)%0A * any later version.
 %0A *
+ 
 %0A * 
-Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,
+This library is distributed in the hope that it will be useful, but
  WIT
@@ -378,16 +378,20 @@
 T%0A *
+ ANY
  WARRANT
 IES 
@@ -390,175 +390,390 @@
 RANT
-IES OR CONDITIONS OF ANY KIND, either express or implied.  See the%0A * License for the specific language governing permissions and limitations%0A * under the License.
+Y; without even the implied warranty of MERCHANTABILITY or FITNESS%0A * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more%0A * details.%0A * %0A * You should have received a copy of the GNU Lesser General Public License%0A * along with this library; if not, write to the Free Software Foundation, Inc.,%0A * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 %0A */
+%0A
 pack

Fix license header