-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from softleader/add-bag-to-context
feat: add bag to conetxt
- Loading branch information
Showing
8 changed files
with
139 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
mapper/src/main/java/tw/com/softleader/data/jpa/spec/SpecJoinContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright © 2022 SoftLeader | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 tw.com.softleader.data.jpa.spec; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import javax.persistence.criteria.Join; | ||
import javax.persistence.criteria.Root; | ||
import lombok.Synchronized; | ||
import lombok.val; | ||
import org.springframework.data.util.Pair; | ||
import tw.com.softleader.data.jpa.spec.domain.JoinContext; | ||
|
||
class SpecJoinContext implements JoinContext { | ||
|
||
private final Map<Pair<String, Root<?>>, Join<?, ?>> joins = new HashMap<>(); | ||
private final Map<String, Function<Root<?>, Join<?, ?>>> lazyJoins = new HashMap<>(); | ||
|
||
@Override | ||
@Synchronized | ||
public Join<?, ?> get(String key, Root<?> root) { | ||
val lazyJoin = lazyJoins.get(key); | ||
if (lazyJoin == null) { | ||
return null; | ||
} | ||
Pair<String, Root<?>> rootKey = Pair.of(key, root); | ||
joins.computeIfAbsent(rootKey, k -> lazyJoin.apply(root)); | ||
return joins.get(rootKey); | ||
} | ||
|
||
public void putLazy(String key, Function<Root<?>, Join<?, ?>> lazyJoin) { | ||
lazyJoins.put(key, lazyJoin); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
mapper/src/main/java/tw/com/softleader/data/jpa/spec/domain/JoinContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright © 2022 SoftLeader | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you 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 tw.com.softleader.data.jpa.spec.domain; | ||
|
||
import java.util.function.Function; | ||
import javax.persistence.criteria.Join; | ||
import javax.persistence.criteria.Root; | ||
|
||
/** | ||
* Share data between specifications | ||
* | ||
* @author Matt Ho | ||
*/ | ||
public interface JoinContext { | ||
|
||
@SuppressWarnings({ "rawtypes" }) | ||
Join get(String key, Root<?> root); | ||
|
||
void putLazy(String key, Function<Root<?>, Join<?, ?>> function); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters