我有一个滑块最多可以显示 3 个元素。如果有更多元素,这些元素将被隐藏,
可以通过单击左右来查看,如下图所示。
该滑块将接收一个位置支柱。请注意,我无法更改此外部组件。
<Slider position={position} />
Run Code Online (Sandbox Code Playgroud)
位置值决定红色位置,如下图所示。
我正在尝试使用一种算法来按照以下规则设置此位置。
如果元素为 3 个或更少,则位置应始终为 0。
更重要的是,它不应该滑过最后一个元素。
这意味着,假设我传入值 4(即索引 4,第 5 个元素)作为位置值,该组件将如下所示。
上面是错误的。它应该如下所示,其中填充了 3 个位置,同时仍然尽可能向左滑动。
因此,如果我将位置值传递为 2,效果会很好。
如果可以接受,也可以按原样使用位置值,而无需按如下方式进行修改。
寻找能够:
获取一个位置值,根据元素数量和最大可见元素数(3)来决定是否可以直接使用还是需要修改。并获取修改后的位置值并将其传递给 Slider 组件。
正在思考以下内容。
根据测试,相信这项工作。正在寻找验证这是否可行。
并建议是否有一种方法可以使其更优雅,并且可能没有
初始 if 检查小于或等于 3 个元素。
// elementCount = // dynamic count of elements.
// initialPosition = // initial position being passed in which needs to be validated.
const getPosition = (elementCount, initialPosition) => {
if (elementCount <= 3) {
return 0;
}
const maxPosition = elementCount - …
Run Code Online (Sandbox Code Playgroud) 有没有办法使用三元运算执行插入到 Postgres 数据库?
我尝试不编写 SQL 函数,也不通过代码传入 current_timestamp 值。试图直接在 SQL 语句本身中实现这一点。这可能吗?
看到了选择查询的示例,其中我可以使用 CASE 和 WHEN,但不能使用插入,并且我没有看到 WHERE 子句适用于此处。
如果可以实现以下建议,请提供建议。谢谢。
对于下面的 check_time 值,我想根据同一查询中的 status 值传入 current_timestamp 或 null 。
INSERT INTO user(name, age, status, check_time) VALUES (?, ?, ?, current_timestamp);
Run Code Online (Sandbox Code Playgroud)
我想将上面修改为以下内容。
这不是一个有效的 SQL 语句,但试图实现此三元组的功能,即当状态为 1 时插入 current_timestamp,否则插入空值。
INSERT INTO user(name, age, status, check_time) VALUES (?, ?, ?, status == 1 ? current_timestamp : null);
Run Code Online (Sandbox Code Playgroud) 我有以下方法可以正常工作。我正在尝试测试抛出InterruptedException的情况。这就是我目前正在测试的方式,如果我仅运行此单个测试,它将起作用。但是,如果我要在测试类中运行所有剩余的5个测试,则某些测试将失败。当我分别运行它们时,所有测试都会通过,因此很明显,我在测试中的线程中断正在影响其他测试。如何编写不会影响其他测试的测试?
@Component
class A{
@Autowired
private Helper helper;
private static ExecutorService executor = Executors.newFixedThreadPool(10);
// static variable in class
private final Future<String> number = executor.submit(() -> helper.method());
//method to be tested
public String getNumber() {
try {
return this.number.get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new CustomException1();
} catch (ExecutionException e) {
throw new CustomException2();
}
}
}
@RunWith(MockitoJUnitRunner.class)
clas ATest{
@InjectMocks
private A a;
@Mock
private Helper helper;
// this is my test method which passes when ran …
Run Code Online (Sandbox Code Playgroud) 如果我尝试推送一个大文件(任何超过 1MB 大小的文件),我的代码最初会被破坏。它现在工作正常,并且能够通过在属性文件中添加以下内容来适应我想要的文件大小。
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=10MB
Run Code Online (Sandbox Code Playgroud)
但是我如何对此编写适当的单元/集成测试以确保它允许文件大小高达 10MB?
下面有一个很好的测试示例(已接受的答案),但它使用模拟文件设置进行测试。 使用 Spring MVC Test 对多部分 POST 请求进行单元测试
这是要测试的方法
@PostMapping(path = "/example", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<SomeResponse> upload(@PathVariable(@RequestPart("file") MultipartFile file) {
//we won't even get inside thi method and would fail if the file size is over 1MB previously.
// It works currently when I add files with size above 1MB
// cos I added the above 2 lines (spring.servlet.... in the properties file)
// some logic which …
Run Code Online (Sandbox Code Playgroud) 我有以下方法工作正常。我正在尝试完成所有操作并获取该 Optional 流中的值,而无需进行额外的 if 检查。是否可以在索引 0 处映射并获取 Result 对象?请指教谢谢。
public String getData(HttpEntity<Request> request, String endPoint){
ResponseEntity<Reponse> response =
template.exchange(endPoint, HttpMethod.POST, request, Reponse.class);
List<Result> results = Optional.ofNullable(response)
.map(ResponseEntity::getBody)
.map(Response::getQueryResult)
.map(QueryResult::getResults)
// getResults is an ArrayList of Result Objects. Could I get the Result Object at index 0 here?
// following that I plan to go .map(Result::getValue) here.
.orElse(null);
if(CollectionUtils.isNotEmpty(results)){
return results.get(0).getValue();
}
return null;
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码以某种格式获取当前时间。当我在我的笔记本电脑上测试它时,这在本地工作得非常好。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS z");
ZonedDateTime date = ZonedDateTime.now();
String timeStamp = formatter.format(date);
Run Code Online (Sandbox Code Playgroud)
以上在本地工作,时间戳值采用以下格式:2020-02-24 05:23:20.675 MST
但是当我将其推送到生产时,格式更改为:2020-02-24 05:23:20.675 -07:00
我无权访问生产设置,处理它的团队在另一个时区,现在无法获得它们。相信这是他们最后的一些设置,但有什么我可以做的,格式总是像:2020-02-24 05:23:20.675 MST?
请指教,谢谢。
我有以下嵌套的空检查。试图通过使其可读,Optional
但如何映射第一个元素?
卡在下面,不确定如何映射这条线
vo.getSomething().getAnother().get(0)
Run Code Online (Sandbox Code Playgroud)
我被困在三号线
Optional.of(vo.getSomething)
.map(Something::getAnother)
.map(List<Another>::get(0)) // will not work
Run Code Online (Sandbox Code Playgroud)
这是一个有效的空检查。我正在尝试使用Optional清理它。
if(vo.getSomething() != null){
if(vo.getSomething().getAnother() != null){
if(vo.getSomething().getAnother().get(0) != null){
if(vo.getSomething().getAnother().get(0).getInner() != null){
if(vo.getSomething().getAnother().get(0).getInner() != null){
if(vo.getSomething().getAnother().get(0).getInner().get(0) != null){
return vo.getSomething().getAnother().get(0).getInner().get(0).getProductName();
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud) java ×5
java-8 ×3
algorithm ×1
date ×1
java-time ×1
javascript ×1
optional ×1
postgresql ×1
spring ×1
spring-boot ×1
sql ×1
unit-testing ×1