考虑以下ArrayList:
[0] => Person
[1] => User
[2] => Dummy
Run Code Online (Sandbox Code Playgroud)
如何使用Java流检查此arraylist是否包含除Personor 之外的任何其他对象User?
所以,我可以做一个if语句,如果它包含返回null 只 Person和User,或者返回数组列表本身,如果它包含任何其他对象之外Person或User像这样:
if( /*arrayList contains only Person and User*/ ) {
return null;
}
else {
//arrayList contains other objects besides Person and User
return arrayList;
}
Run Code Online (Sandbox Code Playgroud) 我是Java 8 Streams的新手,并希望将以下代码块转换为Java 8的Stream方式来做同样的事情.
编辑:更新类名称以减少混淆.(删除了Foo,Bar,Baz ...)
ArrayList<PriceList> priceLists = new ArrayList<PriceList>();
// I'm casting to a type-safe List from getObjects()
// -which is a function I dont have access to. Is there a nice
// solution to embed this in the stream-syntax?
List<PriceListGroup> distObjects = (List<PriceListGroup>) objects.get(1).getObjects();
for(PriceListGroup group : distObjects) {
Set<Affiliate> affiliates = group.getAffiliates();
for(Affiliate affiliate : affiliates) {
priceLists.add(affiliate.getPriceList());
}
}
Run Code Online (Sandbox Code Playgroud)
所有帮助和解释表示赞赏
我正在使用该javax.persistence包来映射我的Java类.
我有这样的实体:
public class UserEntity extends IdEntity {
}
Run Code Online (Sandbox Code Playgroud)
它扩展了一个名为的映射超类IdEntity:
@MappedSuperclass
public class IdEntity extends VersionEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
// Getters and setters below...
}
Run Code Online (Sandbox Code Playgroud)
该IdEntity超类扩展了另一个名为映射超类VersionEntity,使所有实体继承版本特性:
@MappedSuperclass
public abstract class VersionEntity {
@Version
private Integer version;
// Getters and setters below...
}
Run Code Online (Sandbox Code Playgroud)
为什么?
因为现在我可以在所有实体的IdEntity类上进行泛型查询,它看起来像这样:(示例)
CriteriaBuilder builder = JPA.em().getCriteriaBuilder();
CriteriaQuery<IdEntity> criteria = builder.createQuery(IdEntity.class);
Run Code Online (Sandbox Code Playgroud)
现在来问题了.
我的一些实体将有created_at和时间戳一样的deleted_at.但并非所有实体.
我可以在我的实体类中提供这些属性,如下所示:
public class UserEntity …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个测试用例,以测试Laravel 4.2中两个Eloquent模型之间的关系的关联和分离
这是我的测试用例:
class BookingStatusSchemaTest extends TestCase
{
private $statusText = "Confirmed";
private $bookingStub;
private $statusStub;
public function testMigrateService()
{
$this->createTestData();
$booking = $this->bookingStub;
$status = $this->statusStub;
/**
* Check that the booking has no status. OK
*/
$this->assertNull($booking->status);
/**
* Check that status has no booking. OK
*/
$this->assertEquals(count($status->bookings), 0);
/**
* Add a status to the booking. OK
*/
$booking->status()->associate($this->statusStub);
/**
* Check that status has a booking. NOT OK - This gives error
*/
$this->assertEquals(count($status->bookings), 1);
/** …Run Code Online (Sandbox Code Playgroud) 我正在尝试模拟 DateFormat 类,因为它在我的单元测试范围内没有任何用途。我正在使用 org.mockito.Mockito 库。
以下代码:
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.junit.Before;
public class someTest {
@Mock
DateFormat formatter;
@Before
public void before() {
MockitoAnnotations.initMocks(this);
when(formatter.format(any(Date.class))).thenReturn("2017-02-06");
}
}
Run Code Online (Sandbox Code Playgroud)
给出以下错误:
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:参数匹配器的使用无效!预计 3 名匹配者,记录 1 名:
-> 在 someTest.before(someTest.java:33)
如果匹配器与原始值组合,则可能会出现此异常: //in Correct: someMethod(anyObject(), "raw String"); 使用匹配器时,所有参数都必须由匹配器提供。例如: //正确:someMethod(anyObject(), eq("String by matcher"));
有关详细信息,请参阅 Matchers 类的 javadoc。
在 java.text.DateFormat.format(来源未知)
在 someTest.before(someTest.java:33)
如何以正确的方式模拟 DateFormat 类?
java ×4
java-8 ×2
java-stream ×2
arraylist ×1
arrays ×1
criteria-api ×1
eloquent ×1
inheritance ×1
jpa ×1
junit ×1
laravel ×1
laravel-4 ×1
mockito ×1
phpunit ×1
unit-testing ×1