Commit 876a3c78 authored by Liang Ding's avatar Liang Ding

#12398 随机图片工具类

parent 1f31d921
/*
* Copyright (c) 2010-2018, b3log.org & hacpai.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 org.b3log.solo.util;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* Image utilities.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Feb 14, 2018
* @since 2.7.0
*/
public final class Images {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(Images.class);
/**
* Gets an image URL randomly. Sees https://github.com/b3log/bing for more details.
*
* @return an image URL
*/
public static final String randImage() {
try {
final long min = DateUtils.parseDate("20171104", new String[]{"yyyyMMdd"}).getTime();
final long max = System.currentTimeMillis();
final long delta = max - min;
final long time = ThreadLocalRandom.current().nextLong(0, delta) + min;
return "https://img.hacpai.com/bing/" + DateFormatUtils.format(time, "yyyyMMdd") + ".jpg";
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Generates random image URL failed", e);
return "https://img.hacpai.com/bing/20171104.jpg";
}
}
/**
* Gets image URLs randomly.
*
* @param n the specified size
* @return image URLs
*/
public static List<String> randomImages(final int n) {
final List<String> ret = new ArrayList<>();
int i = 0;
while (true) {
if (i >= n * 5) {
break;
}
final String url = randImage();
if (!ret.contains(url)) {
ret.add(url);
}
if (ret.size() >= n) {
return ret;
}
i++;
}
return ret;
}
/**
* Private constructor.
*/
private Images() {
}
}
/*
* Copyright (c) 2010-2018, b3log.org & hacpai.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 org.b3log.solo.util;
import org.testng.Assert;
import org.testng.annotations.Test;
import java.util.List;
/**
* {@link org.b3log.solo.util.Images} test case.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Feb 14, 2018
* @since 2.7.0
*/
public final class ImagesTestCase {
/**
* Test method for {@linkplain Images#randImage()}.
*/
@Test
public void randImage() {
final String url = Images.randImage();
Assert.assertEquals(url.length(), "https://img.hacpai.com/bing/20171104.jpg".length());
}
/**
* Test method for {@linkplain Images#randomImages(int)}.
*/
@Test
public void randImages() {
final List<String> urls = Images.randomImages(10);
Assert.assertEquals(urls.size(), 10);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment