Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
solo-1
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
solo-1
Commits
876a3c78
Commit
876a3c78
authored
Feb 14, 2018
by
Liang Ding
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
#12398 随机图片工具类
parent
1f31d921
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
145 additions
and
0 deletions
+145
-0
src/main/java/org/b3log/solo/util/Images.java
src/main/java/org/b3log/solo/util/Images.java
+96
-0
src/test/java/org/b3log/solo/util/ImagesTestCase.java
src/test/java/org/b3log/solo/util/ImagesTestCase.java
+49
-0
No files found.
src/main/java/org/b3log/solo/util/Images.java
0 → 100644
View file @
876a3c78
/*
* 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
()
{
}
}
src/test/java/org/b3log/solo/util/ImagesTestCase.java
0 → 100644
View file @
876a3c78
/*
* 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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment